package com.moleq.logic;

import java.math.BigDecimal;
import java.math.MathContext;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import android.R.integer;
import android.content.Context;

import com.moleq.base.MGposSender;
import com.moleq.dialog.MessageBox;
import com.moleq.dialog.MessageDialog;
import com.moleq.enums.ItemType;
import com.moleq.enums.PaymentType;
import com.moleq.enums.TransactionStatus;
import com.moleq.models.BottleSetting;
import com.moleq.models.DeptSalesRpt;
import com.moleq.models.FixedTotalRpt;
import com.moleq.models.FreeFuncRpt;
import com.moleq.models.FunctionPanel;
import com.moleq.models.GlobalInfo;
import com.moleq.models.GroupSalesRpt;
import com.moleq.models.ItemDiscountSetting;
import com.moleq.models.MultCouponSetting;
import com.moleq.models.MultiTareSalesRpt;
import com.moleq.models.PluSalesRpt;
import com.moleq.models.PosLineCoupon;
import com.moleq.models.PosLineDiscnt;
import com.moleq.models.PosLineItem;
import com.moleq.models.PosLineTares;
import com.moleq.models.PosTrns;
import com.moleq.models.PriceSetting;
import com.moleq.models.SaleListItem;
import com.moleq.models.TareSetting;
import com.moleq.models.VdrCoupon;
import com.moleq.service.MultCouponSettingService;
import com.moleq.service.ServiceManager;
import com.moleq.service.VdrCouponService;
import com.moleq.util.BigDecmialTool;
import com.moleq.util.MoleQUtil;

public class SaleList {

	public List<SaleListItem> mSaleItems;
	private PriceManager mPriceMgr;

	private ServiceManager mServiceMgr;
	private TransactionManager mTransactionMgr;
	private VdrCouponService mVdrCouponService;
	private MultCouponSettingService mMultCouponSettingService;
	private Context mAppContext;
	private List<TareSetting> listTare = new ArrayList<TareSetting>();
	private SimpleDateFormat dateType = new SimpleDateFormat("yyyyMMdd");
	private SimpleDateFormat timeType = new SimpleDateFormat("HHmmss");
	private MGposSender mposSender = MGposSender.getInstance();
	
	public SaleList() {
		mPriceMgr = new PriceManager();
		mSaleItems = new ArrayList<SaleListItem>();
		mServiceMgr = ServiceManager.getInstance();
		mVdrCouponService = mServiceMgr.mVdrCouponService;
		mMultCouponSettingService = mServiceMgr.mMultCouponSettingService;
	}


	public List<SaleListItem> getSaleItems() {
		return mSaleItems;
	}

	public SaleListItem createItem() {
		long no = mServiceMgr.mPosLineItemService.getMaxNo();
		SaleListItem newItem = new SaleListItem();
		newItem.setEntryNum(getItemCount() + 1);
		newItem.setId(generateID());
		newItem.setParentId("");
		newItem.setName("");
		newItem.setWeightDesc("");
		newItem.setPriceDescription("");
		newItem.setPoslineno(no + 1);
		return newItem;
	}

	public void addItem(SaleListItem item) {
		if (item == null || item.getEntryNum() == 0)
			return;
		if(!GlobalInfo.getInstance().isZeroPrice && item.getPrice() == 0) return;
		
		ProcessByPriceSetting(item);

		ProcessByBottleDeposit(item);

	}
	
	public void ProcessByPriceSetting(SaleListItem item) {

		PriceSetting priceSetting = item.getPriceSetting();

		if (priceSetting == null) {

			item.setDisplayContent(item.getName() + " " + item.getWeightDesc());
			addItemToList(item);

		} else {

			mPriceMgr.process(this, item);
		}

	}


	public void ProcessByBottleDeposit(SaleListItem item){

		if (item.getBottleDeposit() > 0 && item.getmBottleSeting() != null) {
			BottleSetting mBottleSetting = item.getmBottleSeting();
			SaleListItem bottleItem = this.createItem();
			bottleItem.setParentId(item.getId());
			bottleItem.setTax1(item.getTax1());
			bottleItem.setTax2(item.getTax2());
			bottleItem.setTax3(item.getTax3());
			bottleItem.setTax4(item.getTax4());
			bottleItem.setTax5(item.getTax5());
			bottleItem.setTax6(item.getTax6());
			bottleItem.setFoodstamp(item.getFoodstamp());
			bottleItem.setUpc("Bottle");
			bottleItem.setNodiscount(true);
			bottleItem.setName(mBottleSetting.getBo_desc());
			bottleItem.setDisplayContent(mBottleSetting.getBo_desc());
			bottleItem.setItemType(ItemType.BOTTLE_DEPOSIT);
			bottleItem.setQty(item.getQty());
			double bottleAmt = BigDecmialTool.mul(item.getQty(), item.getmBottleSeting().getBo_amt());
			bottleItem.setPrice(bottleAmt);
            bottleItem.setRefund(item.isRefund());
            
			addItemToList(bottleItem);

			if (mBottleSetting.isBo_envf_flag()) {
				SaleListItem envfItem = this.createItem();
				envfItem.setParentId(item.getId());
				envfItem.setTax1(mBottleSetting.isBo_envf_tax1());
				envfItem.setTax2(mBottleSetting.isBo_envf_tax2());
				envfItem.setTax3(mBottleSetting.isBo_envf_tax3());
				envfItem.setTax4(mBottleSetting.isBo_envf_tax4());
				envfItem.setTax5(mBottleSetting.isBo_envf_tax5());
				envfItem.setTax6(mBottleSetting.isBo_envf_tax6());
				envfItem.setFoodstamp(mBottleSetting.isBo_envf_fs());
				bottleItem.setNodiscount(true);
				envfItem.setUpc("Environmental Fees");
				envfItem.setName("Environmental Fees " + MoleQUtil.fixNumber(mBottleSetting.getBo_envf_amt()));
				envfItem.setDisplayContent("Environmental Fees " + MoleQUtil.fixNumber(mBottleSetting.getBo_envf_amt()));
				envfItem.setItemType(ItemType.ENVIRONMENTAL_FEES);
				envfItem.setQty(item.getQty());
				double envfItemAmt = BigDecmialTool.mul(item.getQty(), mBottleSetting.getBo_envf_amt());
				envfItem.setPrice(envfItemAmt);
				envfItem.setRefund(item.isRefund());
				
				addItemToList(envfItem);
			}
		}
	}

	public String generateID() {

		return UUID.randomUUID().toString();
	}

	public void addItemToList(SaleListItem item) {
		
		if (item != null) {
			
			mTransactionMgr = TransactionManager.getInstance();

			if (mTransactionMgr.shiftNum >=0) {
				item.shiftTax(mTransactionMgr.shiftNum);
				mTransactionMgr.shiftNum = -1;
			}
			
			if (mTransactionMgr.shiftFs) {
				item.shiftFoodStamp();
				mTransactionMgr.shiftFs = false;
			}
			
			if(item.getItemType() == ItemType.ITEM_DISCOUNT_PERECENT || 
					item.getItemType() == ItemType.ITEM_DISCOUNT_AMOUNT || 
					item.getItemType() == ItemType.DISCOUNT_LINK || 
					item.getItemType() == ItemType.ITEM_DISCOUNT_MANUAL || 
					item.getItemType() == ItemType.ITEM_COUPON){
				if(mSelectIndex +1 > mSaleItems.size()){
					mSaleItems.add(item);
					this.mSelectIndex = mSaleItems.size() - 1;
				}else{
					
					int insertIdx = findItemLastIndex(mSelectIndex) + 1;
					mSaleItems.add(insertIdx ,item);
					mSelectIndex = insertIdx ;
				}
			}else if(item.getItemType() == ItemType.ITEM_COUPON)
				{
					mSelectIndex = getLastRegularItemIdx();
					int insertIdx = findItemLastIndex(mSelectIndex) + 1;
					mSaleItems.add(insertIdx ,item);
					mSelectIndex = insertIdx ;
				
				}else{
				mSaleItems.add(item);
				this.mSelectIndex = mSaleItems.size() - 1;
			}
			
			
			if (mTransactionMgr.status == TransactionStatus.CLOSED) {
				// insert trn
				String currentDate = dateType.format(new Date());
				String currentTime = timeType.format(new Date());
				int openTime = Integer.valueOf(GlobalInfo.getInstance().openTime);
				int closeTime = Integer.valueOf(GlobalInfo.getInstance().closeTime);
				if (closeTime < openTime) {
					if (Integer.valueOf(currentTime) < closeTime) {
						mTransactionMgr.trnDate = MoleQUtil.getNowPreDate();
					} else {
						mTransactionMgr.trnDate = currentDate;
					}
				} else {
					mTransactionMgr.trnDate = currentDate;
				}
				PosTrns posTrns = new PosTrns();
				posTrns.setPt_no(mTransactionMgr.TransactionNo);
				posTrns.setPt_date(mTransactionMgr.trnDate);
				posTrns.setPt_store_no( GlobalInfo.getInstance().storeNo);
				posTrns.setPt_station(GlobalInfo.getInstance().stationNo);
				posTrns.setPt_crt_date(currentDate);
				posTrns.setPt_crt_time(currentTime);
				posTrns.setPt_done_date("");
				posTrns.setPt_done_time("");
				posTrns.setPt_status("P");
				posTrns.setPt_user_id(GlobalInfo.getInstance().cashier);
				posTrns.setPt_total(0);
				posTrns.setPt_tax(0);
				posTrns.setPt_tax_exem(0);
				posTrns.setPt_cmr_birthday(mTransactionMgr.getBirthdayDate());
				posTrns.setPt_type(MoleQUtil.getTransType(mTransactionMgr.TransType));
				posTrns.setPt_manager_id(null);
//				posTrns.setPt_rounding_fl(mTransactionMgr.Rounding_flag);
//				posTrns.setPt_rounding(mTransactionMgr.Rounding);
				
				mServiceMgr.mPosTrnsService.Insert(posTrns);
			
				mTransactionMgr.status = TransactionStatus.STARTED;
				mposSender.Request_ClearDisplay();
				
				String ls_send = "START " + MoleQUtil.frontCompWithZore(GlobalInfo.getInstance().stationNo, 2) 
						+"-"+String.valueOf(mTransactionMgr.TransactionNo)+" " + MoleQUtil.fixDateTime2(currentDate, "yyyy-M-dd", "yyyyMMdd")
				+" " + MoleQUtil.fixDateTime2(currentTime, "HH:mm:ss", "HHmmss");
				
				if (!mTransactionMgr.mLoadPendingItemMode){ 
					mposSender.Request_Transaction_Start(ls_send);
//					MGposSender.getInstance().send(ls_send);
				}
				
			} else {
				// int trn_no = mServiceMgr.mPosTrnsService.LastTrnNo();
				// if (trn_no > 0) {
				// // GlobalInfo.getInstance().trnNo = trn_no;
				// mTransactionMgr.transactionNo = trn_no;
				// }
			}

			if (item.getItemType() == ItemType.DISCOUNT_AMOUNT || 
					item.getItemType() == ItemType.DISCOUNT_PERECENT || 
					item.getItemType() == ItemType.LIMITED_QTY_PRICE_FIX || 
					item.getItemType() == ItemType.PACKAGE_PRICE_FIX || 
					item.getItemType() == ItemType.BOTTLE_DEPOSIT || 
					item.getItemType() == ItemType.ENVIRONMENTAL_FEES) {

			} else {
				long no = 0;
				int maxno = 0;
				double price = 0;
				if (item.getItemType() == ItemType.ITEM_DISCOUNT_AMOUNT || 
						item.getItemType() == ItemType.ITEM_DISCOUNT_PERECENT || 
						item.getItemType() == ItemType.DISCOUNT_LINK ||
						item.getItemType() == ItemType.ITEM_DISCOUNT_MANUAL) {
					
					if (item.getHavepolinediscnt() == false && item.getItemType() != ItemType.DISCOUNT_LINK) {
						maxno = mServiceMgr.mPosLineDiscntService.getMaxNo() +1;
						PosLineDiscnt mPosLineDiscnt = new PosLineDiscnt();
						mPosLineDiscnt.setLd_line_no(item.getPoslineno());
						mPosLineDiscnt.setLd_discnt_no(item.getPoslinediscntno());
						mPosLineDiscnt.setLd_value(item.getItemDisntvalue());
						mServiceMgr.mPosLineDiscntService.Insert(mPosLineDiscnt);
						item.setLd_id(maxno);
					}

				}else if (item.getItemType() == ItemType.ITEM_COUPON) {
					////
					if (item.getHavepolinecoupon() == false) {
						
						maxno = mServiceMgr.mPosLineCouponService.getMaxNo() +1;
						PosLineCoupon mPosLineCoupon = new PosLineCoupon();
						mPosLineCoupon.setLc_line_no(item.getPoslineno());
						mPosLineCoupon.setLc_type(item.getCoupontype());
						mPosLineCoupon.setLc_value(item.getCouponvalue());
						mServiceMgr.mPosLineCouponService.Insert(mPosLineCoupon);
						item.setLc_id(maxno);
					}
					
				} 
				else {
					no = mServiceMgr.mPosLineItemService.getMaxNo();
					price = item.getPrice();
					if (item.getItemType() == ItemType.OPEN_DEPARTMENT) price = item.getPrice();
					PosLineItem posLineItem = new PosLineItem(no + 1, item.getUpc(), item.getQty(), item.getWeight(), price,item.getPoslineType());
					if (item.getTareCount() > 0 && item.getIsWeight() && item.getHavepolinetare() == false) {
						listTare = item.getmTareSetting();
						for (int i = 0; i < listTare.size(); i++) {
							PosLineTares posLineTares = new PosLineTares();
							posLineTares.setLt_line_no(posLineItem.getNo());
							posLineTares.setLt_tare_no(listTare.get(i).getTa_no());
							posLineTares.setLt_qty(listTare.get(i).getQty());
							mServiceMgr.mPosLineTaresService.Insert(posLineTares);
						}
					}
					mServiceMgr.mPosLineItemService.Insert(posLineItem);
				}

			}
			// ===============================
			selectedIndex(this.mSelectIndex);
			if (!mTransactionMgr.mLoadPendingItemMode) {
				mposSender.sendItemContext(item,mposSender.WHITE);
			}
			announceCartChanges();
		}
		
	}

	// find item include child at last index
	public int findItemLastIndex(int position) {
		int idx = -1;
		
		idx = position;
		if (!isRootItem(position)) {
			idx = this.findByParent( getItem(position).getParentId());
		}
		
		String rootId  = getItem(idx).getId();
		for (int i = mSaleItems.size() -1; i > idx; i--) {
			if (mSaleItems.get(i).getParentId().equals(rootId)) {
				idx = i;
				break;
			}
		}
		
		return idx;
	}


	private boolean isRootItem(int position) {
		boolean result = false;
		if (this.getItem(position).getParentId().equals("")) {
			result = true;
		}
		return result;
	}

	public int getItemQtyInList() {
		int count = 0;
		for (int i = 0; i < this.getItemCount(); i++) {
			SaleListItem item = getItem(i);
			if (item.getItemType() == ItemType.DISCOUNT_AMOUNT || item.getItemType() == ItemType.DISCOUNT_PERECENT 
					|| item.getItemType() == ItemType.LIMITED_QTY_PRICE_FIX || item.getItemType() == ItemType.PACKAGE_PRICE_FIX 
					|| item.getItemType() == ItemType.BOTTLE_DEPOSIT || item.getItemType() == ItemType.ENVIRONMENTAL_FEES 
					|| item.getItemType() == ItemType.ITEM_DISCOUNT_AMOUNT || item.getItemType() == ItemType.ITEM_DISCOUNT_PERECENT  
					|| item.getItemType() == ItemType.ITEM_DISCOUNT_MANUAL || item.getItemType() == ItemType.DISCOUNT_LINK) {

			} else {
				count += item.getQty();
			}
		}
		return count;
	}

	public double getItemPriceInList() {
		double price = 0;
		for (int i = 0; i < this.getItemCount(); i++) {
			SaleListItem item = getItem(i);
			if (item.getItemType() == ItemType.DISCOUNT_AMOUNT || item.getItemType() == ItemType.DISCOUNT_PERECENT 
					|| item.getItemType() == ItemType.LIMITED_QTY_PRICE_FIX || item.getItemType() == ItemType.PACKAGE_PRICE_FIX 
					|| item.getItemType() == ItemType.BOTTLE_DEPOSIT || item.getItemType() == ItemType.ENVIRONMENTAL_FEES 
					|| item.getItemType() == ItemType.ITEM_DISCOUNT_AMOUNT || item.getItemType() == ItemType.ITEM_DISCOUNT_PERECENT 
					|| item.getItemType() == ItemType.ITEM_DISCOUNT_MANUAL || item.getItemType() == ItemType.DISCOUNT_LINK
					||item.getItemType() == ItemType.DISCOUNT_LINK|| item.getItemType() == ItemType.OPEN_DEPARTMENT) {

			} else {
				price += getRefundAmountById(item.getUpc());
			}
		}
		return price;
	}

	public void SaveFixedTotalRpt() {
		int store_id, station, gs_qty, trn_count;
		double gs_amt;
		double ns_amt = 0, refund_amt = 0;
		int ns_qty = 0, refund_qty = 0;
		double taxRate1 = 0, taxRate2 = 0, taxRate3 = 0, taxRate4 = 0, taxRate5 = 0, taxRate6 = 0;
		store_id = GlobalInfo.getInstance().storeNo;// TransactionManager.getInstance().getPosSetting().getStore_no();
		station = GlobalInfo.getInstance().stationNo;// (int)TransactionManager.getInstance().getPosSetting().getPs_station();
		String date = TransactionManager.getInstance().trnDate;
		gs_qty = getItemQtyInList();
		gs_amt = BigDecmialTool.round(mTransactionMgr.getSubTotal(), 2);
		ns_qty = 1;
		ns_amt = BigDecmialTool.round(mTransactionMgr.getAmountPayable(), 2);
		if (MoleQUtil.getTransType(mTransactionMgr.TransType).equals("R")) {
			ns_amt = ns_amt * (-1);
		}
		double cash = BigDecmialTool.round(mTransactionMgr.getTenderAmount(PaymentType.CASH) - mTransactionMgr.getChange(), 2);
		double credit = BigDecmialTool.round(mTransactionMgr.getTenderAmount(PaymentType.CREDIT_CARD), 2);
		double debit = BigDecmialTool.round(mTransactionMgr.getTenderAmount(PaymentType.DEBIT_CARD), 2);
		double check = BigDecmialTool.round(mTransactionMgr.getTenderAmount(PaymentType.CHECK), 2);
		double foodstamp = BigDecmialTool.round(mTransactionMgr.getTenderAmount(PaymentType.FOOD_STAMP), 2);
		double ebt = BigDecmialTool.round(mTransactionMgr.getTenderAmount(PaymentType.EBT_CASH), 2);
		double gift = BigDecmialTool.round(mTransactionMgr.getTenderAmount(PaymentType.GIFT_CARD), 2);
//		refund_qty = 0;
//		refund_amt = 0;
		trn_count = 1;

		FixedTotalRpt ftr = ServiceManager.getInstance().mFixedTotalRptService.getTrnCount(date);
		double average = BigDecmialTool.round((ftr.getFt_ns_amt() + ns_amt) / (ftr.getFt_trn_count() + 1), 2); // Amt(Net// Count)
		double discount = BigDecmialTool.round((mTransactionMgr.getTransDiscount() * (-1)) + mTransactionMgr.getItemDiscount(), 2); // Amt(Discount// M/M)
		double coupon = BigDecmialTool.round(mTransactionMgr.getItemCoupon() + mTransactionMgr.getMutlAddValueCoupon(), 2);
		double vdrCoupon = BigDecmialTool.round(mTransactionMgr.getVdrCoupon(), 2);

		for (int i = 0; i < mTransactionMgr.getTaxSetting().size(); i++) {
			double taxRate = mTransactionMgr.getTaxSetting().get(i).getTx_rate();
			switch (i) {
			case 0:
				taxRate1 = taxRate;
				break;
			case 1:
				taxRate2 = taxRate;
				break;
			case 2:
				taxRate3 = taxRate;
				break;
			case 3:
				taxRate4 = taxRate;
				break;
			case 4:
				taxRate5 = taxRate;
				break;
			case 5:
				taxRate6 = taxRate;
				break;
			}
		}
		double taxable_1 = BigDecmialTool.round(getSubPrice(1), 2);
		double tax_1 = BigDecmialTool.round(taxable_1 * taxRate1, 2);
		double tax_exem_1 = mTransactionMgr.exemptTax1able > 0?BigDecmialTool.round(mTransactionMgr.exemptTax1able * taxRate1, 2):0;
		double taxable_2 = BigDecmialTool.round(getSubPrice(2), 2);
		double tax_2 = BigDecmialTool.round(taxable_2 * taxRate2, 2);
		double tax_exem_2 =  mTransactionMgr.exemptTax2able > 0?BigDecmialTool.round(mTransactionMgr.exemptTax2able * taxRate2, 2):0;
		double taxable_3 = BigDecmialTool.round(getSubPrice(3), 2);
		double tax_3 = BigDecmialTool.round(taxable_3 * taxRate3, 2);
		double tax_exem_3 = mTransactionMgr.exemptTax3able > 0?BigDecmialTool.round(mTransactionMgr.exemptTax3able * taxRate3, 2):0;
		double taxable_4 = BigDecmialTool.round(getSubPrice(4), 2);
		double tax_4 = BigDecmialTool.round(taxable_4 * taxRate4, 2);
		double tax_exem_4 = mTransactionMgr.exemptTax4able > 0?BigDecmialTool.round(mTransactionMgr.exemptTax4able * taxRate4, 2):0;
		double taxable_5 = BigDecmialTool.round(getSubPrice(5), 2);
		double tax_5 = BigDecmialTool.round(taxable_5 * taxRate5, 2);
		double tax_exem_5 = mTransactionMgr.exemptTax5able > 0?BigDecmialTool.round(mTransactionMgr.exemptTax5able * taxRate5, 2):0;
		double taxable_6 = BigDecmialTool.round(getSubPrice(6), 2);
		double tax_6 = BigDecmialTool.round(taxable_6 * taxRate6, 2);
		double tax_exem_6 = mTransactionMgr.exemptTax6able > 0?BigDecmialTool.round(mTransactionMgr.exemptTax6able * taxRate6, 2):0;
		String user_id = GlobalInfo.getInstance().cashier;
		
		refund_qty = mTransactionMgr.refundCount;
		refund_amt = mTransactionMgr.getRefundAmount();
		refund_amt = BigDecmialTool.round(refund_amt, 2);
		
		if (MoleQUtil.getTransType(mTransactionMgr.TransType).equals("R")) {

			gs_qty = gs_qty * (-1);
			gs_amt = gs_amt * (-1);
			gs_amt = BigDecmialTool.round(gs_amt, 2);

			refund_qty = 1;
			refund_amt = mTransactionMgr.getSubTotal() * (-1);
			refund_amt = BigDecmialTool.round(refund_amt, 2);

			cash = cash * (-1);
			credit = credit * (-1);
			debit = debit * (-1);
			check = check * (-1);
			foodstamp = foodstamp * (-1);
			ebt = ebt * (-1);
			gift = gift * (-1);
			discount = discount * (-1);
			coupon = coupon * (-1);
			vdrCoupon = vdrCoupon * (-1);

			taxable_1 = taxable_1 * (-1);
			tax_1 = tax_1 * (-1);
			tax_exem_1 = tax_exem_1 * (-1);
			taxable_2 = taxable_2 * (-1);
			tax_2 = tax_2 * (-1);
			tax_exem_2 = tax_exem_2 * (-1);
			taxable_3 = taxable_3 * (-1);
			tax_3 = tax_3 * (-1);
			tax_exem_3 = tax_exem_3 * (-1);
			taxable_4 = taxable_4 * (-1);
			tax_4 = tax_4 * (-1);
			tax_exem_4 = tax_exem_4 * (-1);
			taxable_5 = taxable_5 * (-1);
			tax_5 = tax_5 * (-1);
			tax_exem_5 = tax_exem_5 * (-1);
			taxable_6 = taxable_6 * (-1);
			tax_6 = tax_6 * (-1);
			tax_exem_6 = tax_exem_6 * (-1);
		}

		FixedTotalRpt fixedTotalRpt = new FixedTotalRpt(0, store_id, station, date, gs_qty, gs_amt, ns_qty, ns_amt, cash, credit, debit, check, 
				foodstamp, ebt, gift, refund_qty, refund_amt, trn_count, average, discount, coupon, taxable_1, tax_1, tax_exem_1, taxable_2, 
				tax_2, tax_exem_2, taxable_3, tax_3, tax_exem_3, taxable_4, tax_4, tax_exem_4, taxable_5, tax_5, tax_exem_5, taxable_6, 
				tax_6, tax_exem_6, user_id,mTransactionMgr.Rounding,vdrCoupon);
		ServiceManager.getInstance().mFixedTotalRptService.InsertOrUpdate(fixedTotalRpt);
	}
	
	
	public void SavePluSalesRpt3(){
		int parentQty = 0,dep = 0,childQty = 0;;
		double parentAmt = 0,childAmt = 0,weight = 0;
		String date = TransactionManager.getInstance().trnDate;
		String weight_unit = GlobalInfo.getInstance().weightUnit;
		List<String> list = new ArrayList<String>();
		for (int i = 0; i < mSaleItems.size(); i++) {
			SaleListItem parent = getItem(i);
			ItemType parentType = parent.getItemType();

			if (parent.getParentId().equals("") && 
					!(parentType == ItemType.LIMITED_QTY_PRICE_FIX || parentType == ItemType.PACKAGE_PRICE_FIX ||
					parentType == ItemType.BOTTLE_DEPOSIT || parentType == ItemType.ENVIRONMENTAL_FEES ||
					parentType == ItemType.OPEN_DEPARTMENT || parentType == ItemType.DISCOUNT_AMOUNT || 
					parentType == ItemType.DISCOUNT_PERECENT || parentType == ItemType.DISCOUNT_AMOUNT ||
					parentType == ItemType.DISCOUNT_PERECENT || parentType == ItemType.ITEM_COUPON ||
					parentType == ItemType.VDR_COUPON)) {
				
				String upc = parent.getUpc();
//				weight = parent.getWeight();
				if (list.contains(upc)) continue;
				parentAmt = 0;
				childAmt = 0;
				parentQty = 0;
				childQty = 0;
				weight = 0;
				for (int j = 0; j < mSaleItems.size(); j++) {
					SaleListItem item = getItem(j);
					if (item.getUpc().equals(upc)) {
						if (item.getParentId().equals("")) {
							parentAmt += item.getPrice();
//							parentQty += item.getQty();
							if (!item.getIsWeight()) {
								if (item.isRefund()) {
									int mQty = item.getQty();
									mQty = mQty * (-1);
									parentQty += mQty;
								}else {
									parentQty += item.getQty();
								}
							}else {
								weight += item.getWeight();
							}
						}else{
							ItemType childType = item.getItemType();
							if (childType == ItemType.ITEM_DISCOUNT_PERECENT || childType == ItemType.ITEM_DISCOUNT_AMOUNT 
									|| childType == ItemType.ITEM_DISCOUNT_MANUAL) {
								childAmt += item.getPrice();
								childQty += item.getQty();
							}
						}
					}else if (item.getItemType() == ItemType.LIMITED_QTY_PRICE_FIX) {
						parentAmt += item.getPrice();
					}
				}
				if (MoleQUtil.getTransType(mTransactionMgr.TransType).equals("R")) {
					parentAmt = parentAmt * (-1);
					childAmt = childAmt * (-1);
					parentQty = parentQty * (-1);
					childQty = childQty * (-1);
					weight = weight * (-1);
				}
				//save parent
				PluSalesRpt pluSalesRpt = new PluSalesRpt();
				pluSalesRpt.setus_store_id(GlobalInfo.getInstance().storeNo);
				pluSalesRpt.setus_station(GlobalInfo.getInstance().stationNo);
				pluSalesRpt.setus_date(date);
				pluSalesRpt.setUs_upc_plu(parent.getUpc());
				pluSalesRpt.setus_qty(parentQty);
				if (weight == 0) {
//					pluSalesRpt.setus_wt_unit("");
					pluSalesRpt.setus_wt_unit("");
					pluSalesRpt.setus_weight(0);
				}else {
					if (parentType == ItemType.PACKAGE_ITEM) {
						pluSalesRpt.setus_qty(1);
					}else {
//						pluSalesRpt.setus_qty(0);
					}
				}
				pluSalesRpt.setus_wt_unit(weight_unit);
				if((GlobalInfo.getInstance().isCalcWeight && parentType == ItemType.PACKAGE_ITEM) 
						|| parentType == ItemType.REGULAR || parentType == ItemType.MULTIPLE_PRICE){
					pluSalesRpt.setus_weight(BigDecmialTool.round(weight, 2));
				}else{
					pluSalesRpt.setus_weight(0);
				}
				pluSalesRpt.setus_amt(BigDecmialTool.round(parentAmt,2));
				pluSalesRpt.setUs_type(parent.getInsertType());
				pluSalesRpt.setUs_sub_dep((int)dep);
				mServiceMgr.mPluSalesRptService.InsertOrUpdatePluSalesRpt(pluSalesRpt);
				
				//save child
				if (childAmt > 0 || childQty > 0) {
					pluSalesRpt = new PluSalesRpt();
					pluSalesRpt.setus_store_id(GlobalInfo.getInstance().storeNo);
					pluSalesRpt.setus_station(GlobalInfo.getInstance().stationNo);
					pluSalesRpt.setus_date(date);
					pluSalesRpt.setUs_upc_plu(parent.getUpc());
					pluSalesRpt.setus_qty(childQty);
					pluSalesRpt.setus_wt_unit("");
					pluSalesRpt.setus_weight(0);
					pluSalesRpt.setus_amt(BigDecmialTool.round(childAmt,2));
					pluSalesRpt.setUs_type("1020");
					pluSalesRpt.setUs_sub_dep((int)dep);
					mServiceMgr.mPluSalesRptService.InsertOrUpdatePluSalesRpt(pluSalesRpt);
				}
				list.add(upc);
			}
		}
	}
	
	public void SavePluSalesRpt2(){
		int qty = 0,dep = 0;;
		double amt = 0;
		String date = TransactionManager.getInstance().trnDate;
		String weight_unit = GlobalInfo.getInstance().weightUnit;
		for (int i = 0; i < mSaleItems.size(); i++) {
			SaleListItem parent = getItem(i);
			ItemType parentType = parent.getItemType();
			if (parent.getParentId().equals("") && 
					!(parentType == ItemType.LIMITED_QTY_PRICE_FIX || parentType == ItemType.PACKAGE_PRICE_FIX ||
					parentType == ItemType.BOTTLE_DEPOSIT || parentType == ItemType.ENVIRONMENTAL_FEES ||
					parentType == ItemType.OPEN_DEPARTMENT || parentType == ItemType.DISCOUNT_AMOUNT || 
					parentType == ItemType.DISCOUNT_PERECENT || parentType == ItemType.DISCOUNT_AMOUNT ||
					parentType == ItemType.DISCOUNT_PERECENT || parentType == ItemType.ITEM_COUPON)) {
				
				PluSalesRpt pluSalesRpt = new PluSalesRpt();
				pluSalesRpt.setus_store_id(GlobalInfo.getInstance().storeNo);
				pluSalesRpt.setus_station(GlobalInfo.getInstance().stationNo);
				pluSalesRpt.setus_date(date);
				pluSalesRpt.setUs_upc_plu(parent.getUpc());
				pluSalesRpt.setus_qty(parent.getQty());
				if (parent.getWeight() <= 0) {
					pluSalesRpt.setus_wt_unit("");
					pluSalesRpt.setus_weight(0);
				}else {
					pluSalesRpt.setus_wt_unit(weight_unit);
					pluSalesRpt.setus_weight(parent.getWeight());
				}
				pluSalesRpt.setus_amt(parent.getPrice());
				pluSalesRpt.setUs_type(parent.getInsertType());
				pluSalesRpt.setUs_sub_dep((int)dep);
				mServiceMgr.mPluSalesRptService.InsertOrUpdatePluSalesRpt(pluSalesRpt);
				
				for (int j = i + 1; j < mSaleItems.size(); j++) {
					SaleListItem child = mSaleItems.get(j);
					ItemType childType = child.getItemType();
					if (childType == ItemType.ITEM_DISCOUNT_PERECENT || childType == ItemType.ITEM_DISCOUNT_AMOUNT 
							|| childType == ItemType.ITEM_DISCOUNT_MANUAL) {
						
						amt += child.getPrice();
						qty += child.getQty();
						dep = (int)child.getDep();
						
						pluSalesRpt = new PluSalesRpt();
						pluSalesRpt.setus_store_id(GlobalInfo.getInstance().storeNo);
						pluSalesRpt.setus_station(GlobalInfo.getInstance().stationNo);
						pluSalesRpt.setus_date(date);
						pluSalesRpt.setUs_upc_plu(parent.getUpc());
						pluSalesRpt.setus_qty(qty);
						pluSalesRpt.setus_wt_unit("");
						pluSalesRpt.setus_weight(0);
						pluSalesRpt.setus_amt(amt);
						pluSalesRpt.setUs_type(child.getInsertType());
						pluSalesRpt.setUs_sub_dep((int)dep);
						mServiceMgr.mPluSalesRptService.InsertOrUpdatePluSalesRpt(pluSalesRpt);
					}
					
					if (mSaleItems.get(j).getParentId().equals("")) {
						i = j;
						amt = 0;
						qty = 0;
						break;
					}
				}
			}
		}
	}

	public void SavePluSalesRpt() {
		List<SaleListItem> listUpc = getItemUpcTypeList();
		String upc, date, weight_unit, ls_amt,insert_type;
		ItemType type;
		SaleListItem temp = null;
		int qty = 0;
		double weight = 0, amt = 0,dep = 0;
		date = TransactionManager.getInstance().trnDate;
		weight_unit = GlobalInfo.getInstance().weightUnit;
		for (int i = 0; i < listUpc.size(); i++) {
			temp = listUpc.get(i);
			upc = temp.getUpc();
			type = temp.getItemType();
			dep = temp.getDep();
			insert_type = temp.getInsertType();
			
			if (type == ItemType.LIMITED_QTY_PRICE_FIX || type == ItemType.PACKAGE_PRICE_FIX ||
					type == ItemType.BOTTLE_DEPOSIT || type == ItemType.ENVIRONMENTAL_FEES ||
					type == ItemType.OPEN_DEPARTMENT || type == ItemType.DISCOUNT_AMOUNT || 
					type == ItemType.DISCOUNT_PERECENT || type == ItemType.DISCOUNT_AMOUNT ||
					type == ItemType.DISCOUNT_PERECENT || type == ItemType.ITEM_COUPON) {
			
			}else if (type == ItemType.ITEM_DISCOUNT_PERECENT || type == ItemType.ITEM_DISCOUNT_AMOUNT 
					|| type == ItemType.ITEM_DISCOUNT_MANUAL) {
//				qty = getQtyBydisno(upc);
				qty = getQtyByUPCAndType(upc,insert_type);
//				weight = getWeightByUPC(upc);
//				amt = getAmountByDisNo(upc);
				amt = getDisAmountByUpcAndType(upc, insert_type);
				amt = BigDecmialTool.round(amt, 2);
//				ls_amt = MoleQUtil.fixNumber(amt);
//				amt = Double.valueOf(ls_amt);
				if (MoleQUtil.getTransType(mTransactionMgr.TransType).equals("R")) {
					qty = qty * (-1);
					weight = weight * (-1);
					amt = amt * (-1);
				}

				PluSalesRpt pluSalesRpt = new PluSalesRpt();
				pluSalesRpt.setus_store_id(GlobalInfo.getInstance().storeNo);
				pluSalesRpt.setus_station(GlobalInfo.getInstance().stationNo);
				pluSalesRpt.setus_date(date);
				pluSalesRpt.setUs_upc_plu(upc);
				if (weight <= 0) {
					pluSalesRpt.setus_qty(qty);
					pluSalesRpt.setus_wt_unit("");
				} else {
					pluSalesRpt.setus_qty(0);
					pluSalesRpt.setus_wt_unit(weight_unit);
				}
				pluSalesRpt.setus_weight(BigDecmialTool.round(weight, 2));
				pluSalesRpt.setus_amt(amt);
				pluSalesRpt.setUs_type(insert_type);
				pluSalesRpt.setUs_sub_dep((int)dep);
				mServiceMgr.mPluSalesRptService.InsertOrUpdatePluSalesRpt(pluSalesRpt);
			} else {
				qty = getQtyByUPCAndType(upc,insert_type);
				weight = getWeightByUPCAndType(upc,insert_type);
				amt = getAmountByUPCAndType(upc,insert_type);
				amt = BigDecmialTool.round(amt, 2);
//				ls_amt = MoleQUtil.fixNumber(amt);
//				amt = Double.valueOf(ls_amt);
				if (MoleQUtil.getTransType(mTransactionMgr.TransType).equals("R")) {
					qty = qty * (-1);
					weight = weight * (-1);
					amt = amt * (-1);
				}

				PluSalesRpt pluSalesRpt = new PluSalesRpt();
				pluSalesRpt.setus_store_id(GlobalInfo.getInstance().storeNo);
				pluSalesRpt.setus_station(GlobalInfo.getInstance().stationNo);
				pluSalesRpt.setus_date(date);
				pluSalesRpt.setUs_upc_plu(upc);
				if (weight <= 0) {
					pluSalesRpt.setus_qty(qty);
					pluSalesRpt.setus_wt_unit("");
				} else {
					if (type == ItemType.PACKAGE_ITEM) {
						pluSalesRpt.setus_qty(1);					
					}else{
						pluSalesRpt.setus_qty(0);
					}
					pluSalesRpt.setus_wt_unit(weight_unit);
				}
				if((GlobalInfo.getInstance().isCalcWeight && type == ItemType.PACKAGE_ITEM) || type == ItemType.REGULAR){
					pluSalesRpt.setus_weight(BigDecmialTool.round(weight, 2));
				}else{
					pluSalesRpt.setus_weight(0);
				}
				pluSalesRpt.setus_amt(amt);
				pluSalesRpt.setUs_type(insert_type);
				pluSalesRpt.setUs_sub_dep((int)dep);
				mServiceMgr.mPluSalesRptService.InsertOrUpdatePluSalesRpt(pluSalesRpt);
			}
		}
	}

	public void SaveDepSalesRpt() {
		List<Integer> listDep = getItemDepList();
		int qty = 0;
		double amt = 0;
		int dep = 0;
		for (int i = 0; i < listDep.size(); i++) {
			dep = listDep.get(i);
			qty = getQtyByDep(dep);
			amt = getAmountByDep(dep);
			amt = BigDecmialTool.round(amt, 2);
			if (MoleQUtil.getTransType(mTransactionMgr.TransType).equals("R")) {
				qty = qty * (-1);
				amt = amt * (-1);
			}
			DeptSalesRpt deptSalesRpt = new DeptSalesRpt();
			deptSalesRpt.setDs_store_id(GlobalInfo.getInstance().storeNo);
			deptSalesRpt.setDs_station(GlobalInfo.getInstance().stationNo);
			deptSalesRpt.setDs_date(TransactionManager.getInstance().trnDate);
			deptSalesRpt.setDs_dept(dep);
			deptSalesRpt.setDs_qty(qty);
			deptSalesRpt.setDs_amt(amt);
			deptSalesRpt.setDs_type("1000");
			mServiceMgr.mDeptSalesRptService.save(deptSalesRpt);
		}
	}	
	
	
	
	public void SaveGroupSalesRpt() {
		List<Integer> listGroup = getItemGroupList();
		int qty = 0, group = 0;
		double amt = 0;
		for (int i = 0; i < listGroup.size(); i++) {
			group = listGroup.get(i);
			qty = getQtyByGroup(group);
			amt = getAmountByGroup(group);
			amt = BigDecmialTool.round(amt, 2);
			if (MoleQUtil.getTransType(mTransactionMgr.TransType).equals("R")) {
				qty = qty * (-1);
				amt = amt * (-1);
			}
			GroupSalesRpt groupSalesRpt = new GroupSalesRpt();
			groupSalesRpt.setGs_store_id(GlobalInfo.getInstance().storeNo);
			groupSalesRpt.setGs_station(GlobalInfo.getInstance().stationNo);
			groupSalesRpt.setGs_date(TransactionManager.getInstance().trnDate);
			groupSalesRpt.setGs_group(group);
			groupSalesRpt.setGs_qty(qty);
			groupSalesRpt.setGs_amt(amt);
			mServiceMgr.mGroupSalesRptService.save(groupSalesRpt);
		}
	}

	public void SaveMultiTareSalesRpt() {
		String date, upc;
		int qty, tare_no;
		date = TransactionManager.getInstance().trnDate;
		for (int i = 0; i < this.getItemCount(); i++) {
			SaleListItem item = getItem(i);
			if (item.getTareCount() > 1 && item.getIsWeight()) {
				upc = item.getUpc();
				List<TareSetting> list = item.getmTareSetting();
				if (list != null && list.size() > 0) {
					for (int j = 0; j < list.size(); j++) {
						tare_no = list.get(j).getTa_no();
						qty = list.get(j).getQty();
						if (MoleQUtil.getTransType(mTransactionMgr.TransType).equals("R")) {
							qty = qty * (-1);
						}
						MultiTareSalesRpt multiTareSalesRpt = new MultiTareSalesRpt();
						multiTareSalesRpt.setMt_store_id(GlobalInfo.getInstance().storeNo);
						multiTareSalesRpt.setMt_station(GlobalInfo.getInstance().stationNo);
						multiTareSalesRpt.setMt_date(date);
						multiTareSalesRpt.setMt_upc_plu(upc);
						multiTareSalesRpt.setMt_tare_no(tare_no);
						multiTareSalesRpt.setMt_qty(qty);
						mServiceMgr.mMultiTareSalesRptService.InsertOrUpdate(multiTareSalesRpt);
					}
				}
			}
		}
	}

	public int getQtyByDep(double dep) {		
		int k = 0;
		for (int i = 0; i < getItemCount(); i++) {
			ItemType type = mSaleItems.get(i).getItemType();
				if (mSaleItems.get(i).getMdepartment() == dep) {
					if (type == ItemType.LIMITED_QTY_PRICE_FIX || type == ItemType.PACKAGE_PRICE_FIX ||
							type == ItemType.BOTTLE_DEPOSIT || type == ItemType.ENVIRONMENTAL_FEES || 
							type == ItemType.DISCOUNT_AMOUNT || type == ItemType.DISCOUNT_PERECENT || 
							type == ItemType.DISCOUNT_LINK || type == ItemType.ITEM_DISCOUNT_MANUAL) {

					}else{
					k += mSaleItems.get(i).getQty();
				}
			}
		}
		return k;
	}

	public int getQtyByGroup(int group) {
		int k = 0;
		for (int i = 0; i < getItemCount(); i++) {
			ItemType type = mSaleItems.get(i).getItemType();
				if (mSaleItems.get(i).getDep_group() == group) {
					if (type == ItemType.DISCOUNT_AMOUNT || type == ItemType.DISCOUNT_PERECENT || 
							type == ItemType.LIMITED_QTY_PRICE_FIX || type == ItemType.PACKAGE_PRICE_FIX || 
							type == ItemType.BOTTLE_DEPOSIT || type == ItemType.ENVIRONMENTAL_FEES || 
							type == ItemType.ITEM_DISCOUNT_AMOUNT || type == ItemType.ITEM_DISCOUNT_PERECENT || 
							type == ItemType.DISCOUNT_LINK) {
					}else{
					k += mSaleItems.get(i).getQty();
				}
			}
		}
		return k;
	}

	public double getWeightByUPC(String upc) {
		double weight = 0;
		int j = -1;
		SaleListItem item = null;
		for (int i = 0; i < mSaleItems.size(); i++) {
			item = mSaleItems.get(i);
			if (item.getUpc().equals(upc)) {
				j = findChild(item.getId(), i);
				while (j >= 0) {
					weight += mSaleItems.get(j).getWeight();
					j = findChild(item.getId(), j);
				}
				weight += item.getWeight();
			}
		}
		return weight;
	}
	
	public double getWeightByUPCAndType(String upc,String type) {
		double weight = 0;
		int j = -1;
		SaleListItem item = null;
		for (int i = 0; i < mSaleItems.size(); i++) {
			item = mSaleItems.get(i);
			if (item.getUpc().equals(upc) && item.getInsertType().equals(type)) {
				j = findChild(item.getId(), i);
				while (j >= 0) {
					weight += mSaleItems.get(j).getWeight();
					j = findChild(item.getId(), j);
				}
				weight += item.getWeight();
			}
		}
		return weight;
	}

	public double getAmountByDep(double dep) {
		double resultAmt = 0;
		int j = -1;
		SaleListItem item = null;
		for (int i = 0; i < mSaleItems.size(); i++) {
			item = mSaleItems.get(i);
			ItemType type = mSaleItems.get(i).getItemType();
			if (item.getMdepartment() == dep) {
				if (type == ItemType.LIMITED_QTY_PRICE_FIX || type == ItemType.PACKAGE_PRICE_FIX ||
						type == ItemType.BOTTLE_DEPOSIT || type == ItemType.ENVIRONMENTAL_FEES || 
						type == ItemType.DISCOUNT_AMOUNT || type == ItemType.DISCOUNT_PERECENT || 
						type == ItemType.DISCOUNT_LINK || type == ItemType.ITEM_DISCOUNT_MANUAL) {

				}else{
					j = findChild(item.getId(), i);
					while (j >= 0) {
						resultAmt += mSaleItems.get(j).getPrice();
						j = findChild(item.getId(), j);
					}
					resultAmt += item.getPrice();
				}
			}
		}
		return resultAmt;
	}

	public double getAmountByGroup(int group) {
		double resultAmt = 0;
		int j = -1;
		SaleListItem item = null;
		for (int i = 0; i < mSaleItems.size(); i++) {
			item = mSaleItems.get(i);
			ItemType type = mSaleItems.get(i).getItemType();
			if (item.getDep_group() == group) {
				if (type == ItemType.DISCOUNT_AMOUNT || type == ItemType.DISCOUNT_PERECENT || 
							type == ItemType.LIMITED_QTY_PRICE_FIX || type == ItemType.PACKAGE_PRICE_FIX || 
							type == ItemType.BOTTLE_DEPOSIT || type == ItemType.ENVIRONMENTAL_FEES || 
							type == ItemType.ITEM_DISCOUNT_AMOUNT || type == ItemType.ITEM_DISCOUNT_PERECENT || 
							type == ItemType.DISCOUNT_LINK) {
					}else{
						
					j = findChild(item.getId(), i);
					while (j >= 0) {
						resultAmt += mSaleItems.get(j).getPrice();
						j = findChild(item.getId(), j);
					}
					resultAmt += item.getPrice();
				}
			}
		}
		return resultAmt;
	}
	
	public double getRealPrice(SaleListItem item){
		double resultAmt = 0;
		int j = -1;
		ItemType type = item.getItemType();
		if (type == ItemType.DISCOUNT_AMOUNT || type == ItemType.DISCOUNT_PERECENT || 
				type == ItemType.LIMITED_QTY_PRICE_FIX || type == ItemType.PACKAGE_PRICE_FIX || 
				type == ItemType.BOTTLE_DEPOSIT || type == ItemType.ENVIRONMENTAL_FEES || 
				type == ItemType.ITEM_DISCOUNT_AMOUNT || type == ItemType.ITEM_DISCOUNT_PERECENT || 
				type == ItemType.DISCOUNT_LINK) {
		}else{
			j = findChild(item.getId(), 0);
//			for (int i = 0; i < mSaleItems.size(); i++) {
				while (j >= 0) {
					resultAmt += mSaleItems.get(j).getPrice();
					j = findChild(item.getId(), j);
				}
				resultAmt += item.getPrice();
//			}
		}
		return resultAmt;
	}

	public List<SaleListItem> getItemUpcTypeList() {
		List<SaleListItem> tempList = new ArrayList<SaleListItem>();
		SaleListItem temp = null;
		for (int i = 0; i < mSaleItems.size(); i++) {
			String upc = mSaleItems.get(i).getUpc();
			ItemType type = mSaleItems.get(i).getItemType();
			double dep = mSaleItems.get(i).getDep();
			String us_type = mSaleItems.get(i).getInsertType();
			
			if (mSaleItems.get(i).getPkgItem()) {
				type = ItemType.PACKAGE_ITEM;
			}
			
			if (type == ItemType.DISCOUNT_LINK || type == ItemType.ITEM_DISCOUNT_AMOUNT|| 
					type == ItemType.ITEM_DISCOUNT_PERECENT || type == ItemType.ITEM_DISCOUNT_MANUAL) {
//				upc = String.valueOf(mSaleItems.get(i).getPoslinediscntno());
				int parentId = this.findParent(mSaleItems.get(i));
				if (parentId >= 0) {
					upc = this.getItem(parentId).getUpc();
					mSaleItems.get(i).setUpc(upc);
					dep = this.getItem(parentId).getDep();
				}
			}
			
//			temp = new SaleListItem(upc, type, dep, 0, us_type);
			temp = new SaleListItem();
			temp.setUpc(upc);
			temp.setItemType(type);
			temp.setDep(dep);
			temp.setInsertType(us_type);
			if (tempList.contains(temp)) {
				continue;
			} else {
				tempList.add(temp);
			}
		}
		return tempList;
	}
	
	public List<Integer> getItemDepList() {	
		List<Integer> tempList = new ArrayList<Integer>();
		for (int i = 0; i < mSaleItems.size(); i++) {
			int dep = mSaleItems.get(i).getMdepartment();
			if ( dep <= 0 || tempList.contains(dep)) {
				continue;
			} else {
				tempList.add(dep);
			}
		}
		return tempList;
	}

	public List<Integer> getItemGroupList() {
		List<Integer> tempList = new ArrayList<Integer>();
		for (int i = 0; i < mSaleItems.size(); i++) {
			int group = mSaleItems.get(i).getDep_group();
			if ( group <= 0 || tempList.contains(group)) {
				continue;
			} else {
				tempList.add(group);
			}
		}
		return tempList;
	}

	public void updateItemQty(SaleListItem item, int qty) {
		// need to to
		announceCartChanges();
	}

	public void updateItem(int idx, SaleListItem item) {
		mSaleItems.set(idx, item);
		announceCartChanges();
	}

	private boolean alreadyInCart(SaleListItem item) {
		boolean result = false;
		for (int i = mSaleItems.size(); i >= 0; i--) {
			if (mSaleItems.get(i).getUpc().equals(item.getUpc())) {
				result = true;
				break;
			}
		}
		return result;
	}

	private int findParent(SaleListItem item) {
		int result = -1;

		for (int i = 0; i < mSaleItems.size(); i++) {
			SaleListItem tmp = mSaleItems.get(i);
			if (tmp.getId().equals(item.getParentId())) {
				result = i;
				break;
			}
		}
		return result;
	}

	private int findChild(String id) {
		int result = -1;

		for (int i = mSaleItems.size() - 1; i >= 0; i--) {
			SaleListItem tmp = mSaleItems.get(i);
			if (tmp.getParentId().equals(id)) {
				result = i;
				break;
			}
		}
		return result;
	}

	private int findChild(String id, int start) {
		int result = -1;
		for (int i = start + 1; i < mSaleItems.size(); i++) {
			SaleListItem tmp = mSaleItems.get(i);
			if (tmp.getParentId().equals(id)) {
				result = i;
				break;
			}
		}
		return result;
	}

	private int findChild(String id, ItemType type) {
		int result = -1;

		for (int i = mSaleItems.size() - 1; i >= 0; i--) {
			SaleListItem tmp = mSaleItems.get(i);
			if (tmp.getParentId().equals(id) && tmp.getItemType() == type) {
				result = i;
				break;
			}
		}
		return result;
	}

	private int findAtLast(String upc) {
		int result = -1;
		for (int i = mSaleItems.size() - 1; i >= 0; i--) {
			if (mSaleItems.get(i).getUpc().equals(upc)) {
				result = i;
				break;
			}
		}
		return result;
	}

	private int findAtLast(SaleListItem item) {
		int result = -1;

		for (int i = mSaleItems.size() - 1; i >= 0; i--) {
			SaleListItem tmp = mSaleItems.get(i);
			if (item.getMixMatch().equals("none")) {
				if (tmp.getUpc().equals(item.getUpc()) && tmp.getItemType().equals(item.getItemType())) {
					result = i;
					break;
				}
			} else {
				if (tmp.getMixMatch().equals(item.getMixMatch()) && !tmp.getIsWeight() && tmp.getItemType() != ItemType.PACKAGE_PRICE_FIX) {
					result = i;
					break;
				}
			}

		}
		return result;
	}

	private int findByID(String id) {
		int result = -1;
		for (int i = 0; i < mSaleItems.size(); i++) {
			if (mSaleItems.get(i).getId().equals(id)) {
				result = i;
				break;
			}
		}
		return result;
	}

	public int findByParent(String parentId) {
		int result = -1;
		String parent = null;
		for (int i = 0; i < mSaleItems.size(); i++) {
			parent = mSaleItems.get(i).getId();
			if (parent != null && parent.equals(parentId)) {
				result = i;
				break;
			}
		}
		
		return result;
	}

//	public int getQtyByUPC(String upc) {
//		int k = 0;
//
//		for (int i = 0; i < getItemCount(); i++) {
//			if (mSaleItems.get(i).getUpc().equals(upc)) {
//				k += mSaleItems.get(i).getQty();
//			}
//		}
//
//		return k;
//	}
	
	public int getQtyByUPC(String upc) {
		int k = 0;

		for (int i = 0; i < getItemCount(); i++) {
			if (mSaleItems.get(i).getUpc().equals(upc)) {
				String[] vdrcoupon = mSaleItems.get(i).getVdrCouponBarcode();
				for (int j = 0; j < vdrcoupon.length; j++) {
					if(vdrcoupon[j] == null || vdrcoupon[j].equals("")){
						k++;
					}
				}
//				k += mSaleItems.get(i).getQty();
			}
		}

		return k;
	}
	
	public int getQtyByUPCAndType(String upc,String type) {
		int k = 0;

		for (int i = 0; i < getItemCount(); i++) {
			SaleListItem item = mSaleItems.get(i);
			if (item.getUpc().equals(upc) && item.getInsertType().equals(type)) {
				k += mSaleItems.get(i).getQty();
			}
		}

		return k;
	}
	
	public int getQtyBydisno(String disno) {
		int k = 0;

		for (int i = 0; i < getItemCount(); i++) {
			if (String.valueOf(mSaleItems.get(i).getPoslinediscntno()).equals(disno)) {
				k += mSaleItems.get(i).getQty();
			}
		}

		return k;
	}
	
	public int getQtyByItem(SaleListItem item) {
		int k = 0;

		for (int i = 0; i < getItemCount(); i++) {
			if (mSaleItems.get(i).getUpc().equals(item.getUpc()) && !mSaleItems.get(i).isRefund()) {
				int qty = mSaleItems.get(i).getQty();
				if (mSaleItems.get(i).isRefund()) {
					qty = qty * (-1);
				}
				k += qty;
			}
		}

		return k;
	}
	
	public int getQtyByItemRefund(SaleListItem item) {
		int k = 0;

		for (int i = 0; i < getItemCount(); i++) {
			if (mSaleItems.get(i).getUpc().equals(item.getUpc()) && mSaleItems.get(i).isRefund()) {
				int qty = mSaleItems.get(i).getQty();
				k += qty;
			}
		}

		return k;
	}
	
	////
	public void setPriceQty(SaleListItem item,int limitqty){
		
		int total = getTotalQtyByItem(item);
		total = (total / limitqty) * limitqty;
		for (int i = 0; i < getItemCount(); i++) {
			if (mSaleItems.get(i).getUpc().equals(item.getUpc())) {
				int priceQty = mSaleItems.get(i).getPriceQty();
				int vdrQty = mSaleItems.get(i).getVdrQty();
				
				int qty = mSaleItems.get(i).getQty() - priceQty - vdrQty;

				total = total - qty;
				
				if (total >= 0) {
					mSaleItems.get(i).setPriceQty(priceQty+qty);
				}else{
					mSaleItems.get(i).setPriceQty(priceQty+Math.abs(total));
				}
			}
		}
	}
	
	public void setPriceQtyByMM(List<SaleListItem> items, int limitqty) {
		// TODO Auto-generated method stub
//		totalQtyInList = list.getQtyByItemNoWeight(items);
		int total = getQtyByItemNoWeight(items);
		total = (total / limitqty) * limitqty;
		for (int i = 0; i < getItemCount(); i++) {
			for (int j = 0; j < items.size(); j++) {

				if (mSaleItems.get(i).getUpc().equals(items.get(j).getUpc()) && !mSaleItems.get(i).getIsWeight()) {
					int priceQty = mSaleItems.get(i).getPriceQty();
					int vdrQty = mSaleItems.get(i).getVdrQty();
					
					int qty = mSaleItems.get(i).getQty() - priceQty - vdrQty;
	
					total = total - qty;
					
					if (total >= 0) {
						mSaleItems.get(i).setPriceQty(priceQty+qty);
					}else{
						mSaleItems.get(i).setPriceQty(priceQty+Math.abs(total));
					}
				}
			}
		}
	}
	
	
	public void setLimitPriceQty(SaleListItem item){
			for (int i = 0; i < getItemCount(); i++) {
				if (mSaleItems.get(i).getUpc().equals(item.getUpc())) {
					
					int qty = mSaleItems.get(i).getQty() ;	
					mSaleItems.get(i).setPriceQty(qty);
				}
			}
	}
//	public void setVdrQty(SaleListItem item,int limitqty){
//		
//		int total = getTotalQtyByItem(item);
//		total = (total / limitqty) * limitqty;
//		for (int i = 0; i < getItemCount(); i++) {
//			if (mSaleItems.get(i).getUpc().equals(item.getUpc())) {
//				int priceQty = mSaleItems.get(i).getPriceQty();
//				int vdrQty = mSaleItems.get(i).getVdrQty();
//				
//				int qty = mSaleItems.get(i).getQty() - priceQty - vdrQty;
//
//				total = total - qty;
//				
//				if (total >= 0) {
//					mSaleItems.get(i).setVdrQty(vdrQty+qty);
//				}else{
//					mSaleItems.get(i).setPriceQty(vdrQty+Math.abs(total));
//				}
//			}
//		}
//	}
	
	public void setPriceVdrQtyBeforeDel(SaleListItem item,int limitqty,boolean isPrice){
		
		int total = getTotalPriceQtyByItem(item);
		if (total < limitqty) {
			for (int i = 0; i < getItemCount(); i++) {
				if (mSaleItems.get(i).getUpc().equals(item.getUpc())) {
					mSaleItems.get(i).setPriceQty(0);
				}
			}
		}
	}
	
	///
	//////////getTotalQty
	public int getTotalQtyByItem(SaleListItem item) {
		int k = 0;
		for (int i = 0; i < getItemCount(); i++) {
			if (mSaleItems.get(i).getUpc().equals(item.getUpc())) {
				
				k += mSaleItems.get(i).getUseQty();
			}
		}

		return k;
	}
	
	public int getTotalPriceQtyByItem(SaleListItem item) {
		int k = 0;
		for (int i = 0; i < getItemCount(); i++) {
			if (mSaleItems.get(i).getUpc().equals(item.getUpc())) {
				
				k += mSaleItems.get(i).getPriceQty();
			}
		}

		return k;
	}
	
	public boolean isUseMM(SaleListItem item){
		boolean check = false;
		
		for (int i = 0; i < mSaleItems.size(); i++) {
			String s = mSaleItems.get(i).getMixMatch();
			String s2 =mSaleItems.get(i).getUpc();
			String type = mSaleItems.get(i).getItemType().name();
			if (mSaleItems.get(i).getMixMatch().equals(item.getMixMatch()) && !mSaleItems.get(i).getUpc().equals(item.getUpc()) && mSaleItems.get(i).getItemType() == ItemType.PACKAGE_PRICE ) {
				check = true;
			}
		}
		
		return check;
		
	}
	
	public int getQtyByItemNoWeight(List<SaleListItem> items) {
		int k = 0;

		for (int i = 0; i < getItemCount(); i++) {
			for (int j = 0; j < items.size(); j++) {
				if (mSaleItems.get(i).getUpc().equals(items.get(j).getUpc()) && !mSaleItems.get(i).getIsWeight()) {
					k += mSaleItems.get(i).getQty();
				}
			}

		}

		return k;
	}
	
	public double geTotalByItemNoWeight(List<SaleListItem> items) {
		double k = 0;

		for (int i = 0; i < getItemCount(); i++) {
			for (int j = 0; j < items.size(); j++) {
				if (mSaleItems.get(i).getUpc().equals(items.get(j).getUpc()) && !mSaleItems.get(i).getIsWeight()) {
					k += mSaleItems.get(i).getPrice();
				}
			}

		}

		return k;
	}

	public int getItemCount() {

		int result = 0;
		if (mSaleItems != null && !mSaleItems.isEmpty()) {
			result = mSaleItems.size();
		}
		return result;
	}

	public int getItemCountForReceipt() {
		int result = 0, qty = 0;
		ItemType type;

		for (int i = 0; i < mSaleItems.size(); i++) {
			type = mSaleItems.get(i).getItemType();
			boolean isRefund = mSaleItems.get(i).isRefund();
//			if (type.equals(ItemType.DISCOUNT_AMOUNT.toString()) || type.equals(ItemType.DISCOUNT_PERECENT.toString()) || type.equals(ItemType.LIMITED_QTY_PRICE_FIX.toString()) || type.equals(ItemType.PACKAGE_PRICE_FIX.toString()) || type.equals(ItemType.BOTTLE_DEPOSIT.toString()) || type.equals(ItemType.ENVIRONMENTAL_FEES.toString())
//					|| type.equals(ItemType.ITEM_DISCOUNT_AMOUNT.toString()) || type.equals(ItemType.ITEM_DISCOUNT_PERECENT.toString()) || type.equals(ItemType.DISCOUNT_LINK.toString()) || type.equals(ItemType.ITEM_DISCOUNT_MANUAL.toString())) {
			if(type == ItemType.DISCOUNT_AMOUNT || type == ItemType.DISCOUNT_PERECENT 
				|| type == ItemType.LIMITED_QTY_PRICE_FIX || type == ItemType.PACKAGE_PRICE_FIX 
				|| type == ItemType.BOTTLE_DEPOSIT || type == ItemType.ENVIRONMENTAL_FEES
				|| type == ItemType.ITEM_DISCOUNT_AMOUNT || type == ItemType.ITEM_DISCOUNT_PERECENT
				|| type == ItemType.DISCOUNT_LINK || isRefund){
			} else {
				qty = mSaleItems.get(i).getQty();
				result = qty + result;
			}
		}
		return result;
	}


	public SaleListItem getItem(int i) {
		SaleListItem result = null;
		if (mSaleItems != null && !mSaleItems.isEmpty()) {
			result = mSaleItems.get(i);
		}
		return result;
	}
	
	public double getTaxOfItemByNo(SaleListItem item, int taxNo) {
		double result = 0;
		double p4 = 0;
		p4 = item.getPrice();
		if (!item.getNodiscount() ) {
			if (mTransactionMgr.TransDntType.equals("P")) {
				p4 = p4 * (1 - mTransactionMgr.TransDntValue * 0.01);
			} else if (mTransactionMgr.TransDntType.equals("A")) {

			}
		}
		switch (taxNo) {
		case 1:
			if (item.getTax1())
				result += p4;
			break;
		case 2:
			if (item.getTax2())
				result += p4;
			break;
		case 3:
			if (item.getTax3())
				result += p4;
			break;
		case 4:
			if (item.getTax4())
				result += p4;
			break;
		case 5:
			if (item.getTax5())
				result += p4;
			break;
		case 6:
			if (item.getTax6())
				result += p4;
			break;
		}

		return result;
	}

	public double getSubPrice(int num) {
		double subPrice = 0;
		for (int i = 0; i < getItemCount(); i++) {
			subPrice += this.getTaxOfItemByNo(getItem(i), num);
		}
		if (subPrice < mTransactionMgr.getTaxSetting().get(num-1).getTx_start_amt() ) {
			return 0;
		}
		return subPrice;
	}

	public double getSubPriceExempt(int num ) {
		double subPrice = 0;
		SaleListItem item = null;
		for (int i = 0; i < getItemCount(); i++) {
			item = getItem(i);
			if (item.getFoodstamp()) {
				subPrice = this.getTaxOfItemByNo(item, num);
			}
		}
		return subPrice;
	}

	public double getRefundAmountById(String id) {
		double resultAmt = 0;
		int j = -1;
		SaleListItem item = null;
		for (int i = 0; i < mSaleItems.size(); i++) {
			item = mSaleItems.get(i);
			if (item.getId().equals(id) && item.isRefund()) {
				j = findChild(item.getId(), i);
				while (j >= 0) {
					resultAmt += mSaleItems.get(j).getPrice();					
					j = findChild(item.getId(), j);
				}
				resultAmt += item.getPrice();
			}
		}
		return resultAmt;
	}
	
	public double getAmountByUPCAndType(String upc,String inserttype) {
		double resultAmt = 0;
		int j = -1;
		SaleListItem item = null;
		for (int i = 0; i < mSaleItems.size(); i++) {
			item = mSaleItems.get(i);
			if (item.getUpc().equals(upc) && item.getInsertType().equals(inserttype)) {
				j = findChild(item.getId(), i);
				while (j >= 0) {
					String type = mSaleItems.get(j).getItemType().toString();
					if(!type.equals(ItemType.DISCOUNT_LINK.toString())
						&& !type.equals(ItemType.ITEM_DISCOUNT_AMOUNT.toString())
						&& !type.equals(ItemType.ITEM_DISCOUNT_PERECENT.toString())
						&& !type.equals(ItemType.ITEM_DISCOUNT_MANUAL.toString())){	
						resultAmt += mSaleItems.get(j).getPrice();					
					}
					j = findChild(item.getId(), j);
				}
				resultAmt += item.getPrice();
			}
		}
		return resultAmt;
	}
	
	public double getAmountByDisNo(String disno) {
		double resultAmt = 0;
		int j = -1;
		SaleListItem item = null;
		for (int i = 0; i < mSaleItems.size(); i++) {
			item = mSaleItems.get(i);
			if (String.valueOf(item.getPoslinediscntno()).equals(disno)) {
				j = findChild(item.getId(), i);
				while (j >= 0) {
					resultAmt += mSaleItems.get(j).getPrice();
					j = findChild(item.getId(), j);
				}
				resultAmt += item.getPrice();
			}
		}
		return resultAmt;
	}
	
	public double getDisAmountByUpcAndType(String upc,String inserttype) {
		double resultAmt = 0;
		int j = -1;
		SaleListItem item = null;
		for (int i = 0; i < mSaleItems.size(); i++) {
			item = mSaleItems.get(i);
			if (item.getUpc().equals(upc) && item.getInsertType().equals(inserttype)) {
				j = findChild(item.getId(), i);
				while (j >= 0) {
					resultAmt += mSaleItems.get(j).getPrice();
					j = findChild(item.getId(), j);
				}
				resultAmt += item.getPrice();
			}
		}
		return resultAmt;
	}
	
	public void clear() {
		// TODO Auto-generated method stub
		this.mSelectIndex = -1;
		mSaleItems.clear();

		announceCartChanges();
	}

	public void removeItem(int position) {
		int i = -1;
		SaleListItem item = getItem(position);
		int j = -1;

		j = findByParent(item.getParentId());

		
		if (j >= 0) {

			i = findAtLast(getItem(j));
		} else {
			j = position;
			i = findAtLast(item);
		}

		if (i >= 0 && i > j) {
//			return i;
		} else {
			removeLinkItem(i);

		}

		announceCartChanges();
	}
	
//	public boolean isMultVdrCoupon(int position){
//		SaleListItem item = getItem(position);
//		String[] Vdr =  item.getVdrCouponBarcode();
//		for (int i = 0; i < Vdr.length; i++) {
//			if(Vdr[i] != null && Vdr[i].length() > 0 && Vdr[i].equals("Vdr Coupon") && Vdr[i].equals("Mult Vdr Coupon") && Vdr[i].equals("Add Value")){
//				return true;
//			}
//		}
//		return false;
//	}
	
	
	private void removeParentItem(int position) {
		SaleListItem item = getItem(position);
		int i = -1;
		if (item != null) {
			String parent = item.getParentId();
			i = findByID(parent);
			if (i >= 0) {
				removeChildItem(i);
			}
		}
	}

	private void removeLinkItem(int position) {

		SaleListItem item = getItem(position);
		int i = -1;
		String id;

		id = item.getId();
		i = findChild(id);
		while (i >= 0) {
			removeLinkItem(i);
			i = findChild(id);
		}
		remove(position);
	}

	private void removeChildItem(int position) {

		SaleListItem item = getItem(position);
		int i = -1;

		String id = item.getParentId();

		i = findByParent(id);
		while (i >= 0) {
			removeChildItem(i);
			i = findByParent(id);
		}
		mSaleItems.remove(position);
	}
	
//	public void removeItemByUPC(String upc, int qty) {
//		int remainQty = 0;
//		int i, j;
//		SaleListItem item;
//		int start = 0, end = 0;
//
//		int totalQty = getQtyByUPC(upc);
//		if (qty == 0 || totalQty < qty)
//			return;
//
//		i = findAtLast(upc);
//		if (i >= 0) {
//
//			remainQty = mSaleItems.get(i).getQty() - qty;
//			if (remainQty > 0) {
//				start = i;
//				item = mSaleItems.get(i);
//				item.setQty(remainQty);
//
//				removeItem(i);
//				addItem(item);
//			} else {
//				removeItem(i);
//				remainQty = Math.abs(remainQty);
//				removeItemByUPC(upc, remainQty);
//			}
//
//		}
//
//	}

	public void removeItemByUPC(String upc, int qty) {
		int remainQty = 0;
		int i, j;
		SaleListItem item;
		int start = 0, end = 0;

		int totalQty = getQtyByUPC(upc);
		if (qty == 0 || totalQty < qty)
			return;

		i = findAtLast(upc);
		if (i >= 0) {
			String[] vdrcoupon = mSaleItems.get(i).getVdrCouponBarcode();
			int freeQty = 0,vdrQty = 0;
			for (int k = 0; k < vdrcoupon.length; k++) {
				if(vdrcoupon[k] == null || vdrcoupon[k].equals("")){
					freeQty++;
				}else{
					vdrQty++;
				}
			}

			remainQty = freeQty - qty;
			if (remainQty > 0) {
				start = i;
				item = mSaleItems.get(i);
				String[] temp = new String[(vdrQty + remainQty)];
				for (int k = 0; k < item.getVdrCouponBarcode().length; k++) {
					String[] temp2 = item.getVdrCouponBarcode();
//					 || !temp2[k].equals("")
					if(temp2[k] != null){
						for (int l = 0; l < temp.length; l++) {
							if(temp[l] == null || temp[l].equals("")){
								temp[l] = temp2[k];
								break;
							}
						}
					}
				}
				item.setQty((vdrQty + remainQty));
				item.setVdrCouponBarcode(temp);

				removeItem(i);
				addItem(item);
			}else if(vdrQty > 0){
				item = mSaleItems.get(i);
				String[] temp = new String[(vdrQty + remainQty)];
				for (int k = 0; k < item.getVdrCouponBarcode().length; k++) {
					String[] temp2 = item.getVdrCouponBarcode();
//					 || !temp2[k].equals("")
					if(temp2[k] != null){
						for (int l = 0; l < temp.length; l++) {
							if(temp[l] == null || temp[l].equals("")){
								temp[l] = temp2[k];
								break;
							}
						}
					}
				}
				item.setQty((vdrQty + remainQty));
				item.setVdrCouponBarcode(temp);

				removeItem(i);
				addItem(item);
				remainQty = Math.abs(remainQty);
				removeItemByUPC(upc, remainQty);
			}else {
				removeItem(i);
				remainQty = Math.abs(remainQty);
				removeItemByUPC(upc, remainQty);
			}

		}

	}

	public void remove(int position) {

		SaleListItem item = getItem(position);
		boolean isRefund = item.isRefund();
		
		long no = item.getPoslineno();
		if (item.getItemType() == ItemType.ITEM_DISCOUNT_AMOUNT || item.getItemType() == ItemType.ITEM_DISCOUNT_PERECENT || item.getItemType() == ItemType.DISCOUNT_LINK|| item.getItemType() == ItemType.ITEM_DISCOUNT_MANUAL) {
			mServiceMgr.mPosLineDiscntService.DeleteByDisNo(item.getLd_id());
			GlobalInfo.getInstance().useFuntionsList.add(FreeFuncRpt.FF_ITEM_DISOCUNT);
			mTransactionMgr.SaveFreeFuncRpt(FreeFuncRpt.FF_ITEM_DISOCUNT, 1,Math.abs(item.getPrice()) * -1);
		} else if(item.getItemType() == ItemType.ITEM_COUPON){
			mServiceMgr.mPosLineCouponService.DeleteByCouponNo(item.getLc_id());
			GlobalInfo.getInstance().useFuntionsList.add(FreeFuncRpt.FF_COUPON);
			mTransactionMgr.SaveFreeFuncRpt(FreeFuncRpt.FF_COUPON, 1,Math.abs(item.getPrice()) * -1);
		}else {
			mServiceMgr.mPosLineTaresService.DeleteByNo(no);
			mServiceMgr.mPosLineItemService.DeleteByNo(no);
			// 当item为Refund时，再用void就当Refund多一次
			if (isRefund) {
				if (item.getItemType() == ItemType.REGULAR || item.getItemType() == ItemType.OPEN_DEPARTMENT) {
					GlobalInfo.getInstance().useFuntionsList.add(FreeFuncRpt.FF_REFUND);
					mTransactionMgr.SaveFreeFuncRpt(FreeFuncRpt.FF_REFUND, 1, item.getPrice() * (-1));
					mTransactionMgr.refundCount++;
				}
			}else {
				GlobalInfo.getInstance().useFuntionsList.add(GlobalInfo.getInstance().CurrentFreeFuncID);
				mTransactionMgr.SaveFreeFuncRpt(GlobalInfo.getInstance().CurrentFreeFuncID, 1, 0);
			}
		}

		mSaleItems.remove(position);
		
		if (item.getItemType() == ItemType.PACKAGE_PRICE) {
			PriceSetting priceSetting = item.getPriceSetting();
			setPriceVdrQtyBeforeDel(item,priceSetting.mPackageQty,true);
		}
		
		mposSender.sendItemContext(item,mposSender.RED);
		
		announceCartChanges();

		mSelectIndex = position - 1;
		if (mSelectIndex >= 0) {
			selectedIndex(mSelectIndex);
		}else{
			if (mSaleItems.size() > 0) {
				selectedIndex(mSelectIndex + 1);
			}
		}
	}

	public void removeItemIncludChild(int position) {

		SaleListItem item = getItem(position);
		int i = -1;
		if (item != null) {

			i = findAtLast(item);

			item = getItem(i);
			mSaleItems.remove(i);

			String id = "", parent = "";
			id = item.getId();
			i = findByParent(id);

			if (i == -1) {
				parent = item.getParentId();
				i = findByID(parent);
				while (i >= 0) {
					removeItemIncludChild(i);
					i = findByParent(parent);
				}
				return;
			}

			while (i >= 0) {
				removeItemIncludChild(i);
				i = findByParent(id);
			}
		}
	}

	public void removeInvalidAgeItem(int age) {
		for (int i = 0; i < mSaleItems.size(); i++) {
			if (mSaleItems.get(i).getAge() > age) {
				removeLinkItem(i);
				removeInvalidAgeItem(age);
			}
		}
	}
	
	public void removeInvalidWICItem() {
		for (int i = 0; i < mSaleItems.size(); i++) {
			if (!mSaleItems.get(i).isWic()) {
				removeLinkItem(i);
				removeInvalidWICItem();
			}
		}
	}
	
	public void removeInvalidWICAmountItem() {
		if (mTransactionMgr.getAmountPayable() > mTransactionMgr.CVVWicPrice) {
			int row = getItemCount() - 1;
			if (row < 0)
				return;
			remove(row);
			
			removeInvalidWICAmountItem();
		}
	}
	
	public void removeInvalidCVVItem() {
		for (int i = 0; i < mSaleItems.size(); i++) {
			if (!mSaleItems.get(i).isCvv()) {
				removeLinkItem(i);
				removeInvalidCVVItem();
			}
		}
	}

	public ArrayList<String> getItemUpcList() {
		ArrayList<String> arry = new ArrayList<String>();
		for (int i = 0; i < mSaleItems.size(); i++) {
			String upc = mSaleItems.get(i).getUpc();
			if (arry.contains(upc)) {
				continue;
			} else {
				arry.add(upc);
			}
		}
		return arry;
	}

	public void hightLightBottomItem() {
		this.selectedIndex(mSaleItems.size() - 1);
	}

	private ArrayList<OnItemDisplayListener> onItemDisplayListener = new ArrayList<OnItemDisplayListener>();
	public int mSelectIndex = -1;

	public void register(OnItemDisplayListener onCalcTotallistener) {
		onItemDisplayListener.add(onCalcTotallistener);
	}

	public void unRegister(OnItemDisplayListener onCalcTotallistener) {
		onItemDisplayListener.remove(onCalcTotallistener);
	}

	public void unRegisterAll() {
		onItemDisplayListener.clear();
	}

	public void selectedIndex(int idx) {
		for (OnItemDisplayListener onCalcTotallistener : onItemDisplayListener) {
			if (onCalcTotallistener != null) {
				onCalcTotallistener.onSelectedIndex(idx);
			}
		}

	}

	public void announceCartChanges() {
		for (OnItemDisplayListener listener : onItemDisplayListener) {
			if (listener != null) {
				listener.onAnnounceCartChanges();
			}
		}
		
		if (mSaleItems.size() == 0 ) {
			for (OnItemDisplayListener listener : onItemDisplayListener) {
				if (listener != null) {
					listener.onNoItemInList();
				}
			}
		}
	}

	public interface OnItemDisplayListener {
		// void onTotalChanged();
		void onShowItem(SaleListItem item);

		void onAnnounceCartChanges();

		void onSelectedIndex(int idx);
		
		void onNoItemInList();
	}

	public void removeChildItem(int itemIdx, ItemType type) {
		String id = mSaleItems.get(itemIdx).getId();
		SaleListItem tmp = null;
		for (int i = itemIdx ; i < mSaleItems.size(); i++) {

			tmp = mSaleItems.get(i);
			if (tmp.getParentId().equals(id) && tmp.getItemType() == type) {
				remove(i);
				break;
			}
		}

	}


	public boolean checkIsLastItem(int position) {
		boolean result = false;
		int i = -1;
		SaleListItem item = getItem(position);
		int j = -1;

		j = findByParent(item.getParentId());

		if (j >= 0) {

			i = findAtLast(getItem(j));
		} else {
			j = position;
			i = findAtLast(item);
		}

		if (i >= 0 && i > j) {
			result = false;
		} else {
			result = true;

		}
		return result;
	}


	public boolean lastRowIsSelected() {
		boolean result = false;
		if (mSelectIndex == getItemCount() - 1) {
			result = true;
		}
		return result;
	}

	public int getSelectedRegularItemIdx() {
		int result = -1;
		for (int i = mSelectIndex  ; i >= 0; i--) {
			SaleListItem item = mSaleItems.get(i);
			if (item.getParentId().equals("")) {
				result = i;
				break;
			}
		}
		
		return result;
	}
	
	public int getLastRegularItemIdx() {
		int result = -1;
		for (int i = getItemCount() - 1; i >= 0; i--) {
			SaleListItem item = mSaleItems.get(i);
			if (item.getParentId().equals("") && item.getItemType()!= ItemType.COUPON && item.getItemType()!= ItemType.OPEN_DEPARTMENT) {
				result = i;
				break;
			}
		}
		
		return result;
	}
	
	public SaleListItem getCurrentSelectedItem(){
		SaleListItem resultitem = null;
		if(mSelectIndex >= 0 ){
			resultitem =  mSaleItems.get(mSelectIndex);
		}
		
		return resultitem;
	}


	public boolean canSingleDeleteItem(int position) {

		boolean result = false;
		
		SaleListItem item = getItem(position);
		if(item.getItemType() == ItemType.ITEM_DISCOUNT_MANUAL || item.getItemType() == ItemType.ITEM_COUPON || item.getItemType() == ItemType.VDR_COUPON){
			result = true;
		}
		
		return result;
	}
	
	public void addItemDiscount(double discountValue,ItemDiscountSetting discount,int type){
		int itemIdx = getSelectedRegularItemIdx();
		if (itemIdx >= 0) {
			SaleListItem item = getItem(itemIdx);

			SaleListItem discountAmtItem = createItemDiscount(item,-1,discount,type,discountValue);
			addItemToList(discountAmtItem);
			
			if (MoleQUtil.getTransType(mTransactionMgr.TransType).equals("R")) {
				//refund fushu
				mTransactionMgr.SaveFreeFuncRpt(FreeFuncRpt.FF_ITEM_DISOCUNT, 1, discountAmtItem.getPrice());
			}else{
				//sale zhengshu
				mTransactionMgr.SaveFreeFuncRpt(FreeFuncRpt.FF_ITEM_DISOCUNT, 1, discountAmtItem.getPrice() * -1);
			}
		}
	}


	public SaleListItem createItemDiscount(SaleListItem regularItem,int ld_id,ItemDiscountSetting discount,int dcnttype,double discountValue) {
		
		SaleListItem discntAmtItem = createItem();
		
//		if(discount != null){
			regularItem.setInsertType(MoleQConstants.PLU_REPOT_DISCOUNT);
			discntAmtItem.setItemType(ItemType.ITEM_DISCOUNT_MANUAL);
			discntAmtItem.setUpc(regularItem.getUpc());
			discntAmtItem.setParentId(regularItem.getId());
			discntAmtItem.setPoslineno(regularItem.getPoslineno());
			discntAmtItem.setFoodstamp(regularItem.getFoodstamp());
			boolean followItem = true;
			String type ="";
			double dnt = 0;
			String desc = "Item Discount ";
			if (discount != null) {
				followItem = discount.isDs_follow_item();
				type = discount.getDs_type();
				dnt = discount.getDs_value();
				desc =  discount.getDs_desc();
			}else{
				desc = desc + (dcnttype == -1?GlobalInfo.getInstance().CurrencySign:"") + MoleQUtil.fixNumber(discountValue);
				dnt = discountValue;
			}
			
			if (followItem) {
				discntAmtItem.setTax1(regularItem.getTax1());
				discntAmtItem.setTax2(regularItem.getTax2());
				discntAmtItem.setTax3(regularItem.getTax3());
				discntAmtItem.setTax4(regularItem.getTax4());
				discntAmtItem.setTax5(regularItem.getTax5());
				discntAmtItem.setTax6(regularItem.getTax6());
				discntAmtItem.setPoslinediscntno(dcnttype);
			}else {
				discntAmtItem.setTax1(discount.isDs_tax1());
				discntAmtItem.setTax2(discount.isDs_tax2());
				discntAmtItem.setTax3(discount.isDs_tax3());
				discntAmtItem.setTax4(discount.isDs_tax4());
				discntAmtItem.setTax5(discount.isDs_tax5());
				discntAmtItem.setTax6(discount.isDs_tax6());
//				discntAmtItem.setFoodstamp(discount.isDs_foodstamp());
				discntAmtItem.setPoslinediscntno(discount.getDs_no());
			}
			discntAmtItem.setNodiscount(regularItem.getNodiscount());
			discntAmtItem.setInsertType(MoleQConstants.PLU_ITEM_DISCOUNT);	 
			discntAmtItem.setLd_id(ld_id);

			if (type.equals("P") || dcnttype == 0) {
				dnt = BigDecmialTool.div(dnt, 100);
				dnt = BigDecmialTool.mul(regularItem.getPrice(), dnt);
				if(dnt > 0) dnt = BigDecmialTool.mul(-1, dnt);
				if (discount == null) desc +="%";
			}else if(type.equals("A") || dcnttype == -1) {
				if(dnt > 0) dnt = BigDecmialTool.mul(-1, dnt);
			}
			discntAmtItem.setItemDisntvalue(discountValue);
			discntAmtItem.setName(desc);
			discntAmtItem.setDisplayContent(desc);
			discntAmtItem.setPrice(dnt);
			discntAmtItem.setRefund(regularItem.isRefund());
			discntAmtItem.setQty(1);
//		}
		
		return discntAmtItem;
	}
	
	
	
	public void addItemCoupon(double couponValue,String type){
//		int itemIdx = getLastRegularItemIdx();
		int itemIdx = getSelectedRegularItemIdx();
		if (itemIdx >= 0) {
			SaleListItem item = getItem(itemIdx);

			SaleListItem couponAmtItem = createItemCoupon(item,-1,couponValue,type);
			addItemToList(couponAmtItem);
			if (MoleQUtil.getTransType(mTransactionMgr.TransType).equals("R")) {
				mTransactionMgr.SaveFreeFuncRpt(FreeFuncRpt.FF_COUPON, 1, couponAmtItem.getPrice());
			}else{
				mTransactionMgr.SaveFreeFuncRpt(FreeFuncRpt.FF_COUPON, 1, couponAmtItem.getPrice() * (-1));
			}
		}
	}


	public SaleListItem createItemCoupon(SaleListItem regularItem, int lc_id,
			double value, String type) {
		SaleListItem couponAmtItem = null;
		
		if(regularItem != null){
			couponAmtItem = createItem();
			couponAmtItem.setItemType(ItemType.ITEM_COUPON);
			couponAmtItem.setUpc("Item Coupon");
			couponAmtItem.setParentId(regularItem.getId());
			couponAmtItem.setPoslineno(regularItem.getPoslineno());
			couponAmtItem.setTax1(regularItem.getTax1());
			couponAmtItem.setTax2(regularItem.getTax2());
			couponAmtItem.setTax3(regularItem.getTax3());
			couponAmtItem.setTax4(regularItem.getTax4());
			couponAmtItem.setTax5(regularItem.getTax5());
			couponAmtItem.setTax6(regularItem.getTax6());
			couponAmtItem.setFoodstamp(regularItem.getFoodstamp());
			couponAmtItem.setNodiscount(regularItem.getNodiscount());
			couponAmtItem.setCoupontype(type);
			couponAmtItem.setInsertType(MoleQConstants.PLU_REPOT_NORMAL);	
			couponAmtItem.setLc_id(lc_id);
			String typedesc = "";
			String desc = "Item Coupon " ;
			BigDecimal b1 = new BigDecimal(value);
			double dnt = b1.doubleValue();
			if (type.equals(FunctionPanel.COUPON_PERCENT) || type.equals(FunctionPanel.COUPON_MANUAL_PERCENT)) {
				couponAmtItem.setCouponvalue(dnt);
				dnt = BigDecmialTool.div(dnt, 100);	
				dnt = BigDecmialTool.mul(regularItem.getPrice(), dnt);
				typedesc = "%";
			}

			desc += MoleQUtil.fixNumber(b1.doubleValue())  + typedesc;
			couponAmtItem.setCouponvalue(b1.doubleValue());
			if(dnt > 0) dnt = BigDecmialTool.mul(-1, dnt);
			couponAmtItem.setPrice(dnt);
			couponAmtItem.setRefund(regularItem.isRefund());
			couponAmtItem.setQty(1);
			couponAmtItem.setName(desc);
			couponAmtItem.setDisplayContent(desc);
		}
		
		return couponAmtItem;

	}
	
	public void addVdrCoupon(UPC upcs){
		VdrCoupon mVdrCoupon = mVdrCouponService.findVdrCouponById(upcs.vdr_coupon_code_no);
		String desc = mVdrCoupon.getCv_desc();
		if(desc.equals("Checker Intervention") || desc.equals("Free Merchandise") || desc.equals("Reservered")) return;
		List<SaleListItem> vdrCouponList = new ArrayList<SaleListItem>();
		double couponValue = mVdrCoupon.getCv_value();
		int min = mVdrCoupon.getCv_min_qty();
		String free_qty = mVdrCoupon.getCv_free_qty();
		int more_fl = Integer.valueOf(mVdrCoupon.getCv_more_fl());
		int count = 0;
		List<String> LinkVdrCouponItem = new ArrayList<String>();
		if(more_fl == 1){
			for (int i = 0 ; i < mSaleItems.size(); i++) {
				SaleListItem item = mSaleItems.get(i);
				String[] coupon = item.getVdrCouponBarcode();
				for (int j = 0; j < coupon.length; j++) {
					if(coupon[j] != null && coupon[j].equals(upcs.code)){
						return;
					}
				}
			}
		}
		for (int i = 0 ; i < mSaleItems.size(); i++) {
			SaleListItem item = mSaleItems.get(i);
			String upc = item.getUpc();
			///////
			if (item.getPriceQty() > 0 && (mVdrCoupon.getCv_code_no() == 2 || mVdrCoupon.getCv_code_no() == 14 || mVdrCoupon.getCv_code_no() == 16 || mVdrCoupon.getCv_code_no() == 19)) {
				continue;
			}
			String itemVdrCode = item.getVdr_code();
			if(!item.getIsVdrCoupon() && !item.getPkgItem() && !item.getIsWeight() && !item.isRefund()){
				if(vdrCouponList.size() > 0 && !(free_qty == null || free_qty.equals("")) && itemVdrCode != null){
					if(itemVdrCode.equals(upcs.vdr_code) && vdrCouponList.get(0).getUpc().equals(upc)){
						String[] vdrCoupon = item.getVdrCouponBarcode();
						for (int j = 0; j < vdrCoupon.length; j++) {
							if(vdrCoupon[j] == null || vdrCoupon[j].equals("")){
								vdrCouponList.add(item);
								count++;
							}
						}
					}
//					}else{
//						String[] vdrCoupon = item.getVdrCouponBarcode();
//						for (int j = 0; j < vdrCoupon.length; j++) {
//							if(vdrCoupon[j] == null || vdrCoupon[j].equals("")){
//								vdrCouponList.add(item);
//								count++;
//							}
//						}
//					}
				}else{
					if(itemVdrCode != null && itemVdrCode.equals(upcs.vdr_code)){
						String[] vdrCoupon = item.getVdrCouponBarcode();
						for (int j = 0; j < vdrCoupon.length; j++) {
							if(vdrCoupon[j] == null || vdrCoupon[j].equals("")){
								vdrCouponList.add(item);
								count++;
							}
						}
					}
				}
			}
		}
		if(count >= min){
			ok://跳出多重循环 ok是标记；
			for (int i = 0; i < vdrCouponList.size(); i++) {
				SaleListItem vdrCouponItem = vdrCouponList.get(i);
				LinkVdrCouponItem.add(vdrCouponItem.getId());
				for (int j = 0; j < mSaleItems.size(); j++) {
					SaleListItem saleListItem = mSaleItems.get(j);
					if(vdrCouponItem.getId().equals(saleListItem.getId())){
						if(more_fl == 1){
							String[] coupon = saleListItem.getVdrCouponBarcode();
							for (int k = 0; k < coupon.length; k++) {
								coupon[k] = upcs.code;
							}
							mSaleItems.get(j).setVdrCouponBarcode(coupon);
						}else{
							String[] coupon = saleListItem.getVdrCouponBarcode();
							for (int k = 0; k < coupon.length; k++) {
								if(min > 0){
									if(coupon[k] == null || coupon[k].equals("")){
										coupon[k] = upcs.code;
										min--;
									}
								}else{
									mSaleItems.get(j).setVdrCouponBarcode(coupon);
									break ok;
								}
							}
							mSaleItems.get(j).setVdrCouponBarcode(coupon);
						}
					}
				}
			}
			int index = vdrCouponList.size() -1;
			if (index < 0) {
				index = 0;
			}
			SaleListItem item = vdrCouponList.get(index);
			SaleListItem couponAmtItem;
			if(!(free_qty == null || free_qty.equals(""))){
//				couponAmtItem = createVdrCoupon(item,mVdrCoupon,item.getUnitPrice(),upcs,-1,LinkVdrCouponItem);
				couponAmtItem = createVdrCoupon(item,mVdrCoupon,item.getVdrCouponPrice(),upcs,-1,LinkVdrCouponItem);
			}else{
				couponAmtItem = createVdrCoupon(item,mVdrCoupon,couponValue,upcs,-1,LinkVdrCouponItem);
			}
			
			addItemToList(couponAmtItem);
			boolean IsMultCoupon = false;
			if(free_qty == null || free_qty.equals("")){
				List<String> link = LinkVdrCouponItem;
				multCouponbreak:
				for (int j = 0; j < link.size(); j++) {
					for (int j2 = 0; j2 < mSaleItems.size(); j2++) {
						SaleListItem linkitem = mSaleItems.get(j2);
						if(linkitem.getId().equals(link.get(j))){
							if(linkitem.getIsMultCoupon()){
								IsMultCoupon = true;
								break multCouponbreak;
							}
						}
					}
				}
			}
			if(IsMultCoupon){
				MultCouponSetting mMultCouponSetting = mMultCouponSettingService.findActiveMultCoupon();
				if(mMultCouponSetting != null){
					if(mMultCouponSetting.getMc_max_value() > 0 && mMultCouponSetting.getMc_max_total() > 0){
						if(mMultCouponSetting.getMc_max_value() >= couponValue){
							if(mMultCouponSetting.getMc_max_total() >= couponValue){
								if(mMultCouponSetting.getMc_add_value() > 0){
									double MultCouponValue = mMultCouponSetting.getMc_max_total() - couponValue;
									if(MultCouponValue > (couponValue * (mMultCouponSetting.getMc_mult() - 1))){
										createMultVdrCoupon(couponAmtItem,mVdrCoupon,(couponValue * (mMultCouponSetting.getMc_mult() - 1)),upcs,-1,mMultCouponSetting,item);
										MultCouponValue = MultCouponValue - (couponValue * (mMultCouponSetting.getMc_mult() - 1));
										if(MultCouponValue > mMultCouponSetting.getMc_add_value()){
											createAddValue(couponAmtItem,mVdrCoupon,mMultCouponSetting.getMc_add_value(),upcs,-1,mMultCouponSetting,item);
										}else{
											createAddValue(couponAmtItem,mVdrCoupon,MultCouponValue,upcs,-1,mMultCouponSetting,item);
										}
									}else{
										createMultVdrCoupon(couponAmtItem,mVdrCoupon,MultCouponValue,upcs,-1,mMultCouponSetting,item);
									}
								}
							}
						}
					}
					
					if(mMultCouponSetting.getMc_max_value() > 0 && !(mMultCouponSetting.getMc_max_total() > 0)){
						if(mMultCouponSetting.getMc_max_value() >= couponValue){
							createMultVdrCoupon(couponAmtItem,mVdrCoupon,(couponValue * (mMultCouponSetting.getMc_mult() - 1)),upcs,-1,mMultCouponSetting,item);
							if(mMultCouponSetting.getMc_add_value() > 0){
								createAddValue(couponAmtItem,mVdrCoupon,mMultCouponSetting.getMc_add_value(),upcs,-1,mMultCouponSetting,item);
							}
						}
					}
					
					if(!(mMultCouponSetting.getMc_max_value() > 0) && mMultCouponSetting.getMc_max_total() > 0){
						double MultCouponValue = mMultCouponSetting.getMc_max_total() - couponValue;
						if(MultCouponValue > (couponValue * (mMultCouponSetting.getMc_mult() - 1))){
							if(mMultCouponSetting.getMc_add_value() > 0){
								createMultVdrCoupon(couponAmtItem,mVdrCoupon,(couponValue * (mMultCouponSetting.getMc_mult() - 1)),upcs,-1,mMultCouponSetting,item);
								MultCouponValue = MultCouponValue - (couponValue * (mMultCouponSetting.getMc_mult() - 1));
								if(MultCouponValue > mMultCouponSetting.getMc_add_value()){
									createAddValue(couponAmtItem,mVdrCoupon,mMultCouponSetting.getMc_add_value(),upcs,-1,mMultCouponSetting,item);
								}else{
									createAddValue(couponAmtItem,mVdrCoupon,MultCouponValue,upcs,-1,mMultCouponSetting,item);
								}
							}
							
						}else{
							createMultVdrCoupon(couponAmtItem,mVdrCoupon,MultCouponValue,upcs,-1,mMultCouponSetting,item);
						}
					}
					
					if(!(mMultCouponSetting.getMc_max_value() > 0) && !(mMultCouponSetting.getMc_max_total() > 0)){
						createMultVdrCoupon(couponAmtItem,mVdrCoupon,(couponValue * (mMultCouponSetting.getMc_mult() - 1)),upcs,-1,mMultCouponSetting,item);
						if(mMultCouponSetting.getMc_add_value() > 0){
							createAddValue(couponAmtItem,mVdrCoupon,mMultCouponSetting.getMc_add_value(),upcs,-1,mMultCouponSetting,item);
						}
					}
				}
			}
		}
	}
	
	public SaleListItem createVdrCoupon(SaleListItem regularItem,VdrCoupon mVdrCoupon,
			double value, UPC upcs,int lc_id,List<String> LinkVdrCouponItem) {
		SaleListItem couponAmtItem = null;
		
		if(regularItem != null){
			couponAmtItem = createItem();
			couponAmtItem.setItemType(ItemType.VDR_COUPON);
			couponAmtItem.setUpc(upcs.code);
//			couponAmtItem.setParentId(regularItem.getId());
			couponAmtItem.setPoslineno(regularItem.getPoslineno());
//			if(free){
//				couponAmtItem.setTax1(regularItem.getTax1());
//				couponAmtItem.setTax2(regularItem.getTax2());
//				couponAmtItem.setTax3(regularItem.getTax3());
//				couponAmtItem.setTax4(regularItem.getTax4());
//				couponAmtItem.setTax5(regularItem.getTax5());
//				couponAmtItem.setTax6(regularItem.getTax6());
//			}
			couponAmtItem.setFoodstamp(regularItem.getFoodstamp());
			couponAmtItem.setNodiscount(regularItem.getNodiscount());
//			couponAmtItem.setCoupontype(type);
			couponAmtItem.setInsertType(MoleQConstants.PLU_REPOT_DISCOUNT);	
			couponAmtItem.setLc_id(lc_id);
			couponAmtItem.setIsVdrCoupon(true);
			couponAmtItem.setVdr_code(upcs.vdr_code);
			couponAmtItem.setFam_code(upcs.fam_code);
			couponAmtItem.setVdr_coupon_code_no(upcs.vdr_coupon_code_no);
			couponAmtItem.setLinkVdrCouponItem(LinkVdrCouponItem);
			String typedesc = "";
			String desc = "---Vdr Coupon " + mVdrCoupon.getCv_desc() + " " ;
			String free_qty = mVdrCoupon.getCv_free_qty();
			if(!(free_qty == null || free_qty.equals(""))) {
				value = value * Integer.valueOf(free_qty);
			}
			BigDecimal b1 = new BigDecimal(value);
			double dnt = b1.doubleValue();
			couponAmtItem.setCouponvalue(b1.doubleValue());
			if(dnt > 0) dnt = BigDecmialTool.mul(-1, dnt);
			couponAmtItem.setPrice(dnt);
			couponAmtItem.setRefund(regularItem.isRefund());
			couponAmtItem.setQty(1);
			couponAmtItem.setName(desc);
			couponAmtItem.setDisplayContent(desc);
			couponAmtItem.setVdrCouponBarcode(new String[]{"Vdr Coupon"});
		}
		
		return couponAmtItem;

	}
	
	public void createMultVdrCoupon(SaleListItem vdrCouponItem,VdrCoupon mVdrCoupon,
			double value, UPC upcs,int lc_id,MultCouponSetting mMultCouponSetting,SaleListItem regularItem) {
		SaleListItem couponAmtItem = null;
		
		if(vdrCouponItem != null){
			couponAmtItem = createItem();
			couponAmtItem.setItemType(ItemType.VDR_COUPON);
			couponAmtItem.setUpc(couponAmtItem.getId());
			couponAmtItem.setParentId(vdrCouponItem.getId());
			couponAmtItem.setPoslineno(vdrCouponItem.getPoslineno());
			couponAmtItem.setTax1(regularItem.getTax1());
			couponAmtItem.setTax2(regularItem.getTax2());
			couponAmtItem.setTax3(regularItem.getTax3());
			couponAmtItem.setTax4(regularItem.getTax4());
			couponAmtItem.setTax5(regularItem.getTax5());
			couponAmtItem.setTax6(regularItem.getTax6());
			couponAmtItem.setFoodstamp(vdrCouponItem.getFoodstamp());
			couponAmtItem.setNodiscount(vdrCouponItem.getNodiscount());
			
//			couponAmtItem.setCoupontype(type);
			couponAmtItem.setInsertType(MoleQConstants.PLU_REPOT_DISCOUNT);	
			couponAmtItem.setLc_id(lc_id);
			couponAmtItem.setIsVdrCoupon(true);
			couponAmtItem.setVdr_code(upcs.vdr_code);
			couponAmtItem.setFam_code(upcs.fam_code);
			couponAmtItem.setVdr_coupon_code_no(upcs.vdr_coupon_code_no);
//			couponAmtItem.setLinkVdrCouponItem(LinkVdrCouponItem);
			String typedesc = "";
			String desc = "";
			if(mMultCouponSetting.getMc_cpn_name() != null && !mMultCouponSetting.getMc_cpn_name().trim().equals("")){
				desc = "---" + mMultCouponSetting.getMc_cpn_name() + " ";
			}else{
				desc = "---" + mMultCouponSetting.getMc_mult() + "cpn ";
			}
//			String free_qty = mVdrCoupon.getCv_free_qty();
//			if(!(free_qty == null || free_qty.equals(""))) {
//				value = value * Integer.valueOf(free_qty);
//			}
			BigDecimal b1 = new BigDecimal(value);
			double dnt = b1.doubleValue();
			couponAmtItem.setCouponvalue(b1.doubleValue());
			if(dnt > 0) dnt = BigDecmialTool.mul(-1, dnt);
			couponAmtItem.setPrice(dnt);
			couponAmtItem.setRefund(vdrCouponItem.isRefund());
			couponAmtItem.setQty(1);
			couponAmtItem.setName(desc);
			couponAmtItem.setDisplayContent(desc);
			couponAmtItem.setVdrCouponBarcode(new String[]{"Mult Vdr Coupon"});
		}
		addItemToList(couponAmtItem);
	}
	
	public void createAddValue(SaleListItem vdrCouponItem,VdrCoupon mVdrCoupon,
			double value, UPC upcs,int lc_id,MultCouponSetting mMultCouponSetting,SaleListItem regularItem) {
		SaleListItem couponAmtItem = null;
		
		if(vdrCouponItem != null){
			couponAmtItem = createItem();
			couponAmtItem.setItemType(ItemType.VDR_COUPON);
			couponAmtItem.setUpc(couponAmtItem.getId());
			couponAmtItem.setParentId(vdrCouponItem.getId());
			couponAmtItem.setPoslineno(vdrCouponItem.getPoslineno());
			couponAmtItem.setTax1(regularItem.getTax1());
			couponAmtItem.setTax2(regularItem.getTax2());
			couponAmtItem.setTax3(regularItem.getTax3());
			couponAmtItem.setTax4(regularItem.getTax4());
			couponAmtItem.setTax5(regularItem.getTax5());
			couponAmtItem.setTax6(regularItem.getTax6());
			couponAmtItem.setFoodstamp(vdrCouponItem.getFoodstamp());
			couponAmtItem.setNodiscount(vdrCouponItem.getNodiscount());
			
//			couponAmtItem.setCoupontype(type);
			couponAmtItem.setInsertType(MoleQConstants.PLU_REPOT_DISCOUNT);	
			couponAmtItem.setLc_id(lc_id);
			couponAmtItem.setIsVdrCoupon(true);
			couponAmtItem.setVdr_code(upcs.vdr_code);
			couponAmtItem.setFam_code(upcs.fam_code);
			couponAmtItem.setVdr_coupon_code_no(upcs.vdr_coupon_code_no);
//			couponAmtItem.setLinkVdrCouponItem(LinkVdrCouponItem);
			String typedesc = "";
			String desc = "---Add Value " ;
			String free_qty = mVdrCoupon.getCv_free_qty();
//			if(!(free_qty == null || free_qty.equals(""))) {
//				value = value * Integer.valueOf(free_qty);
//			}
			BigDecimal b1 = new BigDecimal(value);
			double dnt = b1.doubleValue();
			couponAmtItem.setCouponvalue(b1.doubleValue());
			if(dnt > 0) dnt = BigDecmialTool.mul(-1, dnt);
			couponAmtItem.setPrice(dnt);
			couponAmtItem.setRefund(vdrCouponItem.isRefund());
			couponAmtItem.setQty(1);
			couponAmtItem.setName(desc);
			couponAmtItem.setDisplayContent(desc);
			couponAmtItem.setVdrCouponBarcode(new String[]{"Add Value"});
		}
		addItemToList(couponAmtItem);
	}


	public void addGlobalDiscount(double parm) {
		// TODO Auto-generated method stub
		mTransactionMgr.TransDntType = "P";
		mTransactionMgr.TransDntValue = parm;
		if (MoleQUtil.getTransType(mTransactionMgr.TransType).equals("R")) {
			mTransactionMgr.SaveFreeFuncRpt(FreeFuncRpt.FF_DISCOUNT, 1, mTransactionMgr.getTransDiscount());
		}else {
			mTransactionMgr.SaveFreeFuncRpt(FreeFuncRpt.FF_DISCOUNT, 1, mTransactionMgr.getTransDiscount() * -1);
		}
	}
	
	public void removeGlobalDiscount() {
		// TODO Auto-generated method stub
		if (MoleQUtil.getTransType(mTransactionMgr.TransType).equals("R")) {
			mTransactionMgr.SaveFreeFuncRpt(FreeFuncRpt.FF_DISCOUNT, 1, mTransactionMgr.getTransDiscount() * -1);
		}else {
			mTransactionMgr.SaveFreeFuncRpt(FreeFuncRpt.FF_DISCOUNT, 1, mTransactionMgr.getTransDiscount());
		}
		mTransactionMgr.TransDntType = "";
		mTransactionMgr.TransDntValue = 0;
	}


	public void useTaxExempt(int parm2) {
		// TODO Auto-generated method stub
		for (int i = 0; i < getItemCount(); i++) {
			getItem(i).getExemptTaxnum(parm2);
		}
		selectedIndex(getSelectedRegularItemIdx());
		mTransactionMgr.announceChanges();
	}


	public double getMMPriceByItem(String mmno, double mPackagePrice) {
		// TODO Auto-generated method stub
		double resutl = 0;
		for (int i = 0; i < mSaleItems.size(); i++) {
			if (mSaleItems.get(i).getMixMatch().equals(mmno) && mSaleItems.get(i).getItemType() == ItemType.PACKAGE_PRICE_FIX) {
				resutl += mSaleItems.get(i).getPrice()-mPackagePrice;
			}
		}
		return resutl;
	}


	

}
